﻿Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.ComponentModel.Design.Serialization
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Globalization
Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design
Imports System.Workflow.ComponentModel.Serialization

Namespace System.Workflow.Activities
    <ComVisible(False), DesignerSerializer(GetType(StateDesignerLayoutSerializer), GetType(WorkflowMarkupSerializer)), ActivityDesignerTheme(GetType(StateDesignerTheme))> _
    Friend Class StateDesigner
        Inherits FreeformActivityDesigner
        ' Methods
        Friend Sub AddChild(ByVal child As Activity)
            If (TypeOf MyBase.Activity Is CompositeActivity AndAlso (Not child Is Nothing)) Then
                Dim count As Integer = Me.ContainedDesigners.Count
                Dim insertLocation As New HitTestInfo(Me, HitTestLocations.Designer)
                CompositeActivityDesigner.InsertActivities(Me, insertLocation, New List([Of] Activity)(New Activity() { child }).AsReadOnly, String.Format(CultureInfo.InvariantCulture, DR.GetString("AddingChild"), New Object() { child.GetType.Name }))
                If ((Me.ContainedDesigners.Count > count) AndAlso (Me.ContainedDesigners.Count > 0)) Then
                    Me.ContainedDesigners.Item((Me.ContainedDesigners.Count - 1)).EnsureVisible
                End If
                Me.SelectionService.SetSelectedComponents(New Object() { child }, SelectionTypes.Click)
            End If
        End Sub

        Public Overrides Function CanBeParentedTo(ByVal parentActivityDesigner As CompositeActivityDesigner) As Boolean
            If (parentActivityDesigner Is Nothing) Then
                Throw New ArgumentNullException("parentActivityDesigner")
            End If
            Dim activity As CompositeActivity = [TryCast](parentActivityDesigner.Activity,CompositeActivity)
            Return (((Not activity Is Nothing) AndAlso TypeOf activity Is StateActivity) AndAlso MyBase.CanBeParentedTo(parentActivityDesigner))
        End Function

        Protected Overrides Function CanConnect(ByVal source As ConnectionPoint, ByVal target As ConnectionPoint) As Boolean
            Dim point As DesignerLayoutConnectionPoint = [TryCast](source,DesignerLayoutConnectionPoint)
            Dim point2 As DesignerLayoutConnectionPoint = [TryCast](target,DesignerLayoutConnectionPoint)
            If (point Is Nothing) Then
                If Not Me.IsValidTargetConnectionPoint(source) Then
                    Return False
                End If
            ElseIf TypeOf point.DesignerLayout.ActivityDesigner Is StateFinalizationDesigner Then
                Return False
            End If
            If (point2 Is Nothing) Then
                If Not Me.IsValidTargetConnectionPoint(target) Then
                    Return False
                End If
            ElseIf TypeOf point2.DesignerLayout.ActivityDesigner Is StateFinalizationDesigner Then
                Return False
            End If
            Return (((point Is Nothing) AndAlso (Not point2 Is Nothing)) OrElse ((Not point Is Nothing) AndAlso (point2 Is Nothing)))
        End Function

        Private Function CanDrop(ByVal e As ActivityDragEventArgs) As Boolean
            If (e.Activities.Count = 0) Then
                Return False
            End If
            If Me.HasActiveDesigner Then
                Return False
            End If
            If Not Me.CanInsertActivities(New HitTestInfo(Me, HitTestLocations.Designer), e.Activities) Then
                Return False
            End If
            If (((e.KeyState And 8) <> 8) AndAlso ((e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move)) Then
                Dim moveLocation As New HitTestInfo(Me, HitTestLocations.Designer)
                Dim activity As Activity
                For Each activity In e.Activities
                    If (Not activity.Site Is Nothing) Then
                        Dim designer As ActivityDesigner = StateDesigner.GetDesigner(activity)
                        If (((designer Is Nothing) OrElse (designer.ParentDesigner Is Nothing)) OrElse Not designer.ParentDesigner.CanMoveActivities(moveLocation, New List([Of] Activity)(New Activity() { activity }).AsReadOnly)) Then
                            Return False
                        End If
                    End If
                Next
            End If
            Return True
        End Function

        Public Overrides Function CanInsertActivities(ByVal insertLocation As HitTestInfo, ByVal activitiesToInsert As ReadOnlyCollection([Of] Activity)) As Boolean
            If (Me.HasActiveDesigner OrElse Me.IsStateCustomActivity) Then
                Return False
            End If
            Dim state As StateActivity = DirectCast(MyBase.Activity, StateActivity)
            If (StateMachineHelpers.IsLeafState(state) AndAlso StateMachineHelpers.IsCompletedState(state)) Then
                Return False
            End If
            Dim validChildTypes As ReadOnlyCollection([Of] Type) = Me.ValidChildTypes
            Dim activity2 As Activity
            For Each activity2 In activitiesToInsert
                Dim flag As Boolean = False
                Dim type As Type
                For Each type In validChildTypes
                    If type.IsInstanceOfType(activity2) Then
                        flag = True
                        Exit For
                    End If
                Next
                If Not flag Then
                    Return False
                End If
            Next
            Return MyBase.CanInsertActivities(insertLocation, activitiesToInsert)
        End Function

        Protected Overrides Function CanResizeContainedDesigner(ByVal containedDesigner As ActivityDesigner) As Boolean
            If Me.HasActiveDesigner Then
                Return False
            End If
            Return MyBase.CanResizeContainedDesigner(containedDesigner)
        End Function

        Private Function CheckDragEffect(ByVal e As ActivityDragEventArgs) As DragDropEffects
            If ((e.Activities.Count = 0) OrElse Not Me.DragDropActive) Then
                Return DragDropEffects.None
            End If
            If (((e.KeyState And 8) = 8) AndAlso ((e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy)) Then
                Return DragDropEffects.Copy
            End If
            If ((e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then
                Return DragDropEffects.Move
            End If
            Return e.Effect
        End Function

        Protected Overrides Function CreateConnector(ByVal source As ConnectionPoint, ByVal target As ConnectionPoint) As Connector
            Return New StateDesignerConnector(source, target)
        End Function

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                RemoveHandler Me._designerLinkLayout.MouseDown, New MouseEventHandler(AddressOf Me.StateDesignerLinkMouseDown)
            End If
            MyBase.Dispose(disposing)
        End Sub

        Private Sub EnsureDesignerExtender()
            Dim flag As Boolean = True
            Dim service As IExtenderListService = [TryCast](MyBase.GetService(GetType(IExtenderListService)),IExtenderListService)
            If (Not service Is Nothing) Then
                Dim provider As IExtenderProvider
                For Each provider In service.GetExtenderProviders
                    If (provider.GetType Is GetType(StateDesignerPropertyExtender)) Then
                        flag = False
                        Exit For
                    End If
                Next
            End If
            If flag Then
                Dim service2 As IExtenderProviderService = [TryCast](MyBase.GetService(GetType(IExtenderProviderService)),IExtenderProviderService)
                If (Not service2 Is Nothing) Then
                    service2.AddExtenderProvider(New StateDesignerPropertyExtender)
                    TypeDescriptor.Refresh(MyBase.Activity)
                End If
            End If
        End Sub

        Public Overrides Sub EnsureVisibleContainedDesigner(ByVal containedDesigner As ActivityDesigner)
            If (containedDesigner Is Nothing) Then
                Throw New ArgumentNullException("containedDesigner")
            End If
            MyBase.EnsureVisibleContainedDesigner(containedDesigner)
            If Not Me._ensuringVisible Then
                If TypeOf containedDesigner Is StateDesigner Then
                    Me.SetActiveDesigner(Nothing)
                Else
                    Me.SetActiveDesigner(containedDesigner)
                End If
                Me.SetParentTreeEnsuringVisible(True)
            End If
            Me._ensuringVisible = False
        End Sub

        Private Shared Function FindActivityByQualifiedName(ByVal activity As Activity, ByVal qualifiedName As String) As Activity
            Dim queue As New Queue([Of] Activity)
            queue.Enqueue(activity)
            Do While (queue.Count > 0)
                activity = queue.Dequeue
                If activity.QualifiedName.Equals(qualifiedName) Then
                    Return activity
                End If
                Dim activity2 As CompositeActivity = [TryCast](activity,CompositeActivity)
                If (Not activity2 Is Nothing) Then
                    Dim activity3 As Activity
                    For Each activity3 In activity2.Activities
                        queue.Enqueue(activity3)
                    Next
                End If
            Loop
            Return Nothing
        End Function

        Friend Function FindConnector(ByVal transitionInfo As TransitionInfo) As StateDesignerConnector
            Dim connector As Connector
            For Each connector In MyBase.Connectors
                Dim stateDesignerConnector As StateDesignerConnector = [TryCast](connector,StateDesignerConnector)
                If ((Not stateDesignerConnector Is Nothing) AndAlso transitionInfo.Matches(stateDesignerConnector)) Then
                    Return stateDesignerConnector
                End If
            Next
            Return Nothing
        End Function

        Friend Shared Function GetCompletedStateDesignerImage(ByVal stateDesigner As StateDesigner) As Image
            Dim designerTheme As StateMachineTheme = [TryCast](stateDesigner.DesignerTheme,StateMachineTheme)
            If ((Not designerTheme Is Nothing) AndAlso (Not designerTheme.CompletedStateDesignerImage Is Nothing)) Then
                Return designerTheme.CompletedStateDesignerImage
            End If
            Return StateDesigner.CompletedState
        End Function

        Public Overrides Function GetConnectionPoints(ByVal edges As DesignerEdges) As ReadOnlyCollection([Of] ConnectionPoint)
            Dim list As New List([Of] ConnectionPoint)
            If Not Me.HasActiveDesigner Then
                If Not Me.IsRootStateDesigner Then
                    If ((edges And DesignerEdges.Top) > DesignerEdges.None) Then
                        list.Add(New ConnectionPoint(Me, DesignerEdges.Top, 0))
                    End If
                    If ((edges And DesignerEdges.Bottom) > DesignerEdges.None) Then
                        list.Add(New ConnectionPoint(Me, DesignerEdges.Bottom, 0))
                    End If
                End If
                Dim connectionIndex As Integer = 0
                Dim num2 As Integer = 0
                Dim layout As DesignerLayout
                For Each layout In Me.DesignerLayouts.Values
                    If ((Not Me.IsRootStateDesigner AndAlso ((edges And DesignerEdges.Left) > DesignerEdges.None)) AndAlso (layout.LeftConnectionPoint <> Point.Empty)) Then
                        list.Add(New DesignerLayoutConnectionPoint(Me, connectionIndex, DirectCast(layout.ActivityDesigner.Activity, CompositeActivity), DesignerEdges.Left))
                        connectionIndex += 1
                    End If
                    If (((edges And DesignerEdges.Right) > DesignerEdges.None) AndAlso (layout.RightConnectionPoint <> Point.Empty)) Then
                        list.Add(New DesignerLayoutConnectionPoint(Me, num2, DirectCast(layout.ActivityDesigner.Activity, CompositeActivity), DesignerEdges.Right))
                        num2 += 1
                    End If
                Next
            End If
            Return list.AsReadOnly
        End Function

        Protected Overrides Function GetConnections(ByVal edges As DesignerEdges) As ReadOnlyCollection([Of] Point)
            Dim list As New List([Of] Point)
            If ((edges And DesignerEdges.Top) > DesignerEdges.None) Then
                list.Add(Me.TopConnectionPoint)
            End If
            If ((edges And DesignerEdges.Bottom) > DesignerEdges.None) Then
                list.Add(Me.BottomConnectionPoint)
            End If
            Dim layout As DesignerLayout
            For Each layout In Me.DesignerLayouts.Values
                If ((Not Me.IsRootStateDesigner AndAlso ((edges And DesignerEdges.Left) > DesignerEdges.None)) AndAlso (layout.LeftConnectionPoint <> Point.Empty)) Then
                    list.Add(layout.LeftConnectionPoint)
                End If
                If (((edges And DesignerEdges.Right) > DesignerEdges.None) AndAlso (layout.RightConnectionPoint <> Point.Empty)) Then
                    list.Add(layout.RightConnectionPoint)
                End If
            Next
            Return list.AsReadOnly
        End Function

        Friend Shared Function GetDesigner(ByVal activity As Activity) As ActivityDesigner
            Dim designer As ActivityDesigner = Nothing
            If ((Not activity Is Nothing) AndAlso (Not activity.Site Is Nothing)) Then
                Dim service As IDesignerHost = [TryCast](activity.Site.GetService(GetType(IDesignerHost)),IDesignerHost)
                If (Not service Is Nothing) Then
                    designer = [TryCast](service.GetDesigner(activity),ActivityDesigner)
                End If
            End If
            Return designer
        End Function

        Friend Shared Function GetDesignerImage(ByVal designer As ActivityDesigner) As Image
            If ((Not designer.DesignerTheme Is Nothing) AndAlso (Not designer.DesignerTheme.DesignerImage Is Nothing)) Then
                Return designer.DesignerTheme.DesignerImage
            End If
            If (designer.Image Is Nothing) Then
                Return ActivityToolboxItem.GetToolboxImage(designer.Activity.GetType)
            End If
            Return designer.Image
        End Function

        Private Function GetDragImageSnapPoint(ByVal e As ActivityDragEventArgs) As Point
            Dim point As New Point(e.Y, e.Y)
            If Not Me.HasActiveDesigner Then
                Dim bottom As Integer = Me._statesLayout.EventHandlersLayout.Bounds.Bottom
                If (point.Y <= bottom) Then
                    point.Y = (bottom + 1)
                End If
            End If
            Return point
        End Function

        Private Function GetEventHandlerConnectionPoint(ByVal eventHandler As CompositeActivity) As DesignerLayoutConnectionPoint
            Dim layout As DesignerLayout
            Dim associatedDesigner As StateDesigner = DirectCast(StateDesigner.GetDesigner(eventHandler.Parent), StateDesigner)
            If Not associatedDesigner.DesignerLayouts.TryGetValue(eventHandler, layout) Then
                Return Nothing
            End If
            Dim connectionIndex As Integer = 0
            Dim layout2 As DesignerLayout
            For Each layout2 In associatedDesigner.DesignerLayouts.Values
                If (layout2 Is layout) Then
                    Exit For
                End If
                connectionIndex += 1
            Next
            Return New DesignerLayoutConnectionPoint(associatedDesigner, connectionIndex, eventHandler, DesignerEdges.Right)
        End Function

        Friend Shared Function GetInitialStateDesignerImage(ByVal stateDesigner As StateDesigner) As Image
            Dim designerTheme As StateMachineTheme = [TryCast](stateDesigner.DesignerTheme,StateMachineTheme)
            If ((Not designerTheme Is Nothing) AndAlso (Not designerTheme.InitialStateDesignerImage Is Nothing)) Then
                Return designerTheme.InitialStateDesignerImage
            End If
            Return StateDesigner.InitialState
        End Function

        Private Function GetIsEditable() As Boolean
            Return True
        End Function

        Public Overrides Function GetNextSelectableObject(ByVal current As Object, ByVal direction As DesignerNavigationDirection) As Object
            Dim activity As Activity = [TryCast](current,Activity)
            If (Not activity Is Nothing) Then
                Me.SetParentTreeEnsuringVisible(False)
                Dim item As ActivityDesigner = StateDesigner.GetDesigner(activity)
                Dim ordered As List([Of] ActivityDesigner) = Me.DesignersParser.Ordered
                Dim index As Integer = ordered.IndexOf(item)
                If (index < 0) Then
                    Return Nothing
                End If
                If ((TypeOf current Is EventDrivenActivity OrElse TypeOf current Is StateInitializationActivity) OrElse TypeOf current Is StateFinalizationActivity) Then
                    If ((direction <> DesignerNavigationDirection.Left) AndAlso (direction <> DesignerNavigationDirection.Right)) Then
                        If (direction = DesignerNavigationDirection.Down) Then
                            If (index < (ordered.Count - 1)) Then
                                Return ordered.Item((index + 1)).Activity
                            End If
                            Return Nothing
                        End If
                        If (index > 0) Then
                            Return ordered.Item((index - 1)).Activity
                        End If
                    End If
                    Return Nothing
                End If
                Dim state As StateActivity = [TryCast](current,StateActivity)
                If StateMachineHelpers.IsLeafState(state) Then
                    If (direction <> DesignerNavigationDirection.Right) Then
                        If (direction <> DesignerNavigationDirection.Up) Then
                            If ((direction = DesignerNavigationDirection.Down) AndAlso (index < (ordered.Count - 1))) Then
                                Return ordered.Item((index + 1)).Activity
                            End If
                        ElseIf (index > 0) Then
                            Return ordered.Item((index - 1)).Activity
                        End If
                    ElseIf (Me.DesignersParser.StateDesigners.Count > 0) Then
                        Return Me.DesignersParser.StateDesigners.Item(0).Activity
                    End If
                ElseIf ((direction = DesignerNavigationDirection.Left) OrElse (direction = DesignerNavigationDirection.Up)) Then
                    If (index > 0) Then
                        Return ordered.Item((index - 1)).Activity
                    End If
                ElseIf (index < (ordered.Count - 1)) Then
                    Return ordered.Item((index + 1)).Activity
                End If
            End If
            Return Nothing
        End Function

        Private Shared Function GetPropertyDescriptor(ByVal activity As Activity, ByVal propertyName As String) As PropertyDescriptor
            Return TypeDescriptor.GetProperties(activity).Find(propertyName, False)
        End Function

        Private Shared Function GetService(ByVal designer As ActivityDesigner, ByVal serviceType As Type) As Object
            If (designer Is Nothing) Then
                Throw New ArgumentNullException("designer")
            End If
            If (serviceType Is Nothing) Then
                Throw New ArgumentNullException("serviceType")
            End If
            Dim activity As Activity = designer.Activity
            Dim service As Object = Nothing
            If ((Not activity Is Nothing) AndAlso (Not activity.Site Is Nothing)) Then
                service = activity.Site.GetService(serviceType)
            End If
            Return service
        End Function

        Private Function GetTargetStateConnectionPoint(ByVal targetState As StateActivity) As ConnectionPoint
            Return New ConnectionPoint(DirectCast(StateDesigner.GetDesigner(targetState), StateDesigner), DesignerEdges.Top, 0)
        End Function

        Public Overrides Function HitTest(ByVal point As Point) As HitTestInfo
            Dim info As HitTestInfo = MyBase.HitTest(point)
            Dim info2 As HitTestInfo = Me.RootDesignerLayout.HitTest(point)
            If Not info2.Equals(HitTestInfo.Nowhere) Then
                Return info2
            End If
            Return info
        End Function

        Protected Overrides Sub Initialize(ByVal activity As Activity)
            MyBase.Initialize(activity)
            Me.EnsureDesignerExtender
            Me._titleBarLayout = New TitleBarLayout(Me)
            Me._eventDrivenLayout = New EventDrivenLayout(Me, Me._titleBarLayout)
            Me._eventHandlersLayout = New EventHandlersLayout(Me)
            Me._statesLayout = New StatesLayout(Me, Me._titleBarLayout, Me._eventHandlersLayout)
            Me._designerLinkLayout = New DesignerLinkLayout(Me)
            AddHandler Me._designerLinkLayout.MouseDown, New MouseEventHandler(AddressOf Me.StateDesignerLinkMouseDown)
            MyBase.AutoSizeMargin = New Size(&H10, &H18)
            MyBase.AutoSize = True
        End Sub

        Public Overrides Function IsContainedDesignerVisible(ByVal containedDesigner As ActivityDesigner) As Boolean
            If Me.HasActiveDesigner Then
                If (containedDesigner Is Me.ActiveDesigner) Then
                    Return True
                End If
            ElseIf TypeOf containedDesigner Is StateDesigner Then
                Return True
            End If
            Return False
        End Function

        Private Function IsValidTargetConnectionPoint(ByVal target As ConnectionPoint) As Boolean
            Dim associatedDesigner As StateDesigner = [TryCast](target.AssociatedDesigner,StateDesigner)
            If (associatedDesigner Is Nothing) Then
                Return False
            End If
            Dim state As StateActivity = DirectCast(associatedDesigner.Activity, StateActivity)
            Return StateMachineHelpers.IsLeafState(state)
        End Function

        Friend Sub OnAddEventDriven(ByVal sender As Object, ByVal e As EventArgs)
            Dim child As New EventDrivenActivity
            Me.AddChild(child)
        End Sub

        Friend Sub OnAddState(ByVal sender As Object, ByVal e As EventArgs)
            Dim child As New StateActivity
            Me.AddChild(child)
        End Sub

        Friend Sub OnAddStateFinalization(ByVal sender As Object, ByVal e As EventArgs)
            Dim child As New StateFinalizationActivity
            Me.AddChild(child)
        End Sub

        Friend Sub OnAddStateInitialization(ByVal sender As Object, ByVal e As EventArgs)
            Dim child As New StateInitializationActivity
            Me.AddChild(child)
        End Sub

        Protected Overrides Sub OnConnectorAdded(ByVal e As ConnectorEventArgs)
            MyBase.OnConnectorAdded(e)
            Dim connector As StateDesignerConnector = [TryCast](e.Connector,StateDesignerConnector)
            If (Not connector Is Nothing) Then
                Dim source As DesignerLayoutConnectionPoint = [TryCast](connector.Source,DesignerLayoutConnectionPoint)
                Dim target As ConnectionPoint = connector.Target
                If (source Is Nothing) Then
                    Dim point2 As ConnectionPoint = connector.Source
                    connector.Source = connector.Target
                    connector.Target = point2
                End If
                Dim point3 As ConnectionPoint = connector.Target
                source = DirectCast(connector.Source, DesignerLayoutConnectionPoint)
                If Me.RootStateDesigner.AddingSetState Then
                    Dim activity As New SetStateActivity { _
                        .TargetStateName = point3.AssociatedDesigner.Activity.QualifiedName _
                    }
                    Dim designer As CompositeActivityDesigner = DirectCast(StateDesigner.GetDesigner(source.EventHandler), CompositeActivityDesigner)
                    designer.InsertActivities(New HitTestInfo(designer, HitTestLocations.Designer), New List([Of] Activity)() { activity }.AsReadOnly)
                    connector.SetStateName = activity.QualifiedName
                End If
                connector.TargetStateName = point3.AssociatedDesigner.Activity.QualifiedName
                connector.SourceStateName = source.EventHandler.Parent.QualifiedName
                connector.EventHandlerName = source.EventHandler.QualifiedName
            End If
        End Sub

        Protected Overrides Sub OnConnectorChanged(ByVal e As ConnectorEventArgs)
            MyBase.OnConnectorChanged(e)
            Dim connector As StateDesignerConnector = [TryCast](e.Connector,StateDesignerConnector)
            If (Not connector Is Nothing) Then
                If Not connector.Target.AssociatedDesigner.Activity.QualifiedName.Equals(connector.TargetStateName) Then
                    Dim activity As StateActivity = DirectCast(Me.RootStateDesigner.Activity, StateActivity)
                    Dim component As SetStateActivity = [TryCast](StateDesigner.FindActivityByQualifiedName(activity, connector.SetStateName),SetStateActivity)
                    If (Not component Is Nothing) Then
                        Dim activity3 As StateActivity = DirectCast(connector.Target.AssociatedDesigner.Activity, StateActivity)
                        StateDesigner.GetPropertyDescriptor(component, "TargetStateName").SetValue(component, activity3.QualifiedName)
                        connector.TargetStateName = activity3.QualifiedName
                    End If
                End If
                Dim source As DesignerLayoutConnectionPoint = DirectCast(connector.Source, DesignerLayoutConnectionPoint)
                If Not source.EventHandler.QualifiedName.Equals(connector.EventHandlerName) Then
                    Dim activity4 As StateActivity = DirectCast(Me.RootStateDesigner.Activity, StateActivity)
                    Dim activity5 As SetStateActivity = [TryCast](StateDesigner.FindActivityByQualifiedName(activity4, connector.SetStateName),SetStateActivity)
                    If (Not activity5 Is Nothing) Then
                        Dim service As IDesignerHost = [TryCast](MyBase.GetService(GetType(IDesignerHost)),IDesignerHost)
                        Dim transaction As DesignerTransaction = Nothing
                        If (Not service Is Nothing) Then
                            transaction = service.CreateTransaction(SR.GetMoveSetState)
                        End If
                        Try 
                            DirectCast(StateDesigner.GetDesigner(activity5.Parent), CompositeActivityDesigner).RemoveActivities(New List([Of] Activity)() { activity5 }.AsReadOnly)
                            Dim point2 As DesignerLayoutConnectionPoint = DirectCast(connector.Source, DesignerLayoutConnectionPoint)
                            Dim designer As CompositeActivityDesigner = DirectCast(StateDesigner.GetDesigner(point2.EventHandler), CompositeActivityDesigner)
                            designer.InsertActivities(New HitTestInfo(designer, HitTestLocations.Designer), New List([Of] Activity)() { activity5 }.AsReadOnly)
                            connector.EventHandlerName = point2.EventHandler.QualifiedName
                            connector.SourceStateName = point2.EventHandler.Parent.QualifiedName
                            If (Not transaction Is Nothing) Then
                                transaction.Commit
                            End If
                        Catch obj1 As Object
                            If (Not transaction Is Nothing) Then
                                transaction.Cancel
                            End If
                            Throw
                        End Try
                    End If
                End If
            End If
        End Sub

        Protected Overrides Sub OnConnectorRemoved(ByVal e As ConnectorEventArgs)
            MyBase.OnConnectorRemoved(e)
            Dim connector As StateDesignerConnector = [TryCast](e.Connector,StateDesignerConnector)
            If (((Not connector Is Nothing) AndAlso Not String.IsNullOrEmpty(connector.SetStateName)) AndAlso Me.RootStateDesigner.RemovingSetState) Then
                Dim source As DesignerLayoutConnectionPoint = [TryCast](connector.Source,DesignerLayoutConnectionPoint)
                If (((Not source Is Nothing) AndAlso TypeOf StateDesigner.GetDesigner(source.EventHandler) Is CompositeActivityDesigner) AndAlso (Not source.EventHandler Is Nothing)) Then
                    Dim activity As Activity = StateDesigner.FindActivityByQualifiedName(source.EventHandler, connector.SetStateName)
                    If (Not activity Is Nothing) Then
                        Dim list As New List([Of] Activity) { activity }
                        Dim designer As CompositeActivityDesigner = [TryCast](StateDesigner.GetDesigner(activity.Parent),CompositeActivityDesigner)
                        If (Not designer Is Nothing) Then
                            designer.RemoveActivities(list.AsReadOnly)
                        End If
                    End If
                End If
            End If
        End Sub

        Protected Overrides Sub OnContainedActivitiesChanged(ByVal listChangeArgs As ActivityCollectionChangeEventArgs)
            MyBase.OnContainedActivitiesChanged(listChangeArgs)
            If ((Not Me.ActiveDesigner Is Nothing) AndAlso listChangeArgs.RemovedItems.Contains(Me.ActiveDesigner.Activity)) Then
                Me.SetActiveDesigner(Nothing)
            End If
            Me.DesignersParser = Nothing
        End Sub

        Protected Overrides Sub OnDragDrop(ByVal e As ActivityDragEventArgs)
            If Me.DragDropActive Then
                MyBase.OnDragDrop(e)
                Me.DragDropActive = False
            End If
        End Sub

        Protected Overrides Sub OnDragEnter(ByVal e As ActivityDragEventArgs)
            MyBase.OnDragEnter(e)
            Me.DragDropActive = Me.CanDrop(e)
            e.Effect = Me.CheckDragEffect(e)
            e.DragImageSnapPoint = Me.GetDragImageSnapPoint(e)
        End Sub

        Protected Overrides Sub OnDragLeave()
            MyBase.OnDragLeave
            Me.DragDropActive = False
        End Sub

        Protected Overrides Sub OnDragOver(ByVal e As ActivityDragEventArgs)
            MyBase.OnDragOver(e)
            Me.DragDropActive = Me.CanDrop(e)
            e.Effect = Me.CheckDragEffect(e)
            e.DragImageSnapPoint = Me.GetDragImageSnapPoint(e)
        End Sub

        Friend Function OnGetPropertyValue(ByVal extendedProperty As ExtendedPropertyInfo, ByVal extendee As Object) As Object
            Dim obj2 As Object = Nothing
            If extendedProperty.Name.Equals("Location", StringComparison.Ordinal) Then
                Return If((Me.ActiveDesigner Is Nothing), Me.Location, Me._stateLocation)
            End If
            If extendedProperty.Name.Equals("Size", StringComparison.Ordinal) Then
                obj2 = If((Me.ActiveDesigner Is Nothing), Me.Size, Me._stateSize)
            End If
            Return obj2
        End Function

        Protected Overrides Sub OnLayoutPosition(ByVal e As ActivityDesignerLayoutEventArgs)
            Try 
                If (Me.IsRootStateDesigner AndAlso (Me.ActiveDesigner Is Nothing)) Then
                    Me.UpdateConnectors
                End If
                Dim graphics As Graphics = e.Graphics
                Dim designerTheme As ActivityDesignerTheme = e.DesignerTheme
                Dim ambientTheme As AmbientTheme = e.AmbientTheme
                Me.RootDesignerLayout.Location = Me.Location
                Me.RootDesignerLayout.OnLayoutPosition(graphics, designerTheme, ambientTheme)
                If Not Me.HasActiveDesigner Then
                    Me.RelocateStates
                End If
                MyBase.OnLayoutPosition(e)
                If (Not Me.HasActiveDesigner AndAlso Me.NeedsAutoLayout) Then
                    Me.RepositionStates
                End If
                If (MyBase.IsRootDesigner AndAlso (Me.InvokingDesigner Is Nothing)) Then
                    Me.RecalculateRootDesignerSize
                End If
            Finally
                If Me.IsRootStateDesigner Then
                    Me.PerformingLayout = False
                End If
            End Try
        End Sub

        Protected Overrides Function OnLayoutSize(ByVal e As ActivityDesignerLayoutEventArgs) As Size
            If Me.IsRootStateDesigner Then
                Me.PerformingLayout = True
            End If
            If Me.HasActiveDesigner Then
                Me._minimumSize = Size.Empty
                Me.Size = Size.Empty
            Else
                Me.NeedsAutoLayout = Me.Size.IsEmpty
            End If
            Dim containerSize As Size = MyBase.OnLayoutSize(e)
            Dim graphics As Graphics = e.Graphics
            Dim designerTheme As ActivityDesignerTheme = e.DesignerTheme
            Dim ambientTheme As AmbientTheme = e.AmbientTheme
            Me.RefreshRootDesignerLayout
            Me.RootDesignerLayout.OnLayoutSize(graphics, designerTheme, ambientTheme, containerSize)
            Me._minimumSize = Me.RootDesignerLayout.MinimumSize
            Return Me.RootDesignerLayout.Size
        End Function

        Protected Overrides Sub OnMouseDoubleClick(ByVal e As MouseEventArgs)
            MyBase.OnMouseDoubleClick(e)
            Me.RootDesignerLayout.OnMouseDoubleClick(e)
        End Sub

        Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
            MyBase.OnMouseDown(e)
            Me.RootDesignerLayout.OnMouseDown(e)
        End Sub

        Protected Overrides Sub OnMouseLeave()
            MyBase.OnMouseLeave
            Me.RootDesignerLayout.OnMouseLeave
        End Sub

        Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
            MyBase.OnMouseMove(e)
            Me.RootDesignerLayout.OnMouseMove(e)
        End Sub

        Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
            MyBase.OnMouseDown(e)
            Me.RootDesignerLayout.OnMouseUp(e)
        End Sub

        Protected Overrides Sub OnPaint(ByVal e As ActivityDesignerPaintEventArgs)
            Dim graphics As Graphics = e.Graphics
            Dim designerTheme As ActivityDesignerTheme = e.DesignerTheme
            Dim ambientTheme As AmbientTheme = e.AmbientTheme
            Me.RootDesignerLayout.OnPaint(graphics, designerTheme, ambientTheme)
            MyBase.PaintContainedDesigners(e)
        End Sub

        Friend Sub OnSetAsCompletedState(ByVal sender As Object, ByVal e As EventArgs)
            Dim service As IDesignerHost = [TryCast](MyBase.GetService(GetType(IDesignerHost)),IDesignerHost)
            Dim transaction As DesignerTransaction = Nothing
            Dim state As StateActivity = DirectCast(MyBase.Activity, StateActivity)
            If (Not service Is Nothing) Then
                transaction = service.CreateTransaction(SR.GetUndoSetAsCompletedState(state.Name))
            End If
            Try 
                Dim rootState As StateActivity = StateMachineHelpers.GetRootState(state)
                StateDesigner.GetPropertyDescriptor(rootState, "CompletedStateName").SetValue(rootState, state.Name)
                If (StateMachineHelpers.GetInitialStateName(rootState) = state.Name) Then
                    StateDesigner.GetPropertyDescriptor(rootState, "InitialStateName").SetValue(rootState, "")
                End If
                If (Not transaction Is Nothing) Then
                    transaction.Commit
                End If
            Catch obj1 As Object
                If (Not transaction Is Nothing) Then
                    transaction.Cancel
                End If
                Throw
            End Try
        End Sub

        Friend Sub OnSetAsInitialState(ByVal sender As Object, ByVal e As EventArgs)
            Dim service As IDesignerHost = [TryCast](MyBase.GetService(GetType(IDesignerHost)),IDesignerHost)
            Dim transaction As DesignerTransaction = Nothing
            Dim state As StateActivity = DirectCast(MyBase.Activity, StateActivity)
            If (Not service Is Nothing) Then
                transaction = service.CreateTransaction(SR.GetUndoSetAsInitialState(state.Name))
            End If
            Try 
                Dim rootState As StateActivity = StateMachineHelpers.GetRootState(state)
                StateDesigner.GetPropertyDescriptor(rootState, "InitialStateName").SetValue(rootState, state.Name)
                If (StateMachineHelpers.GetCompletedStateName(rootState) = state.Name) Then
                    StateDesigner.GetPropertyDescriptor(rootState, "CompletedStateName").SetValue(rootState, "")
                End If
                If (Not transaction Is Nothing) Then
                    transaction.Commit
                End If
            Catch obj1 As Object
                If (Not transaction Is Nothing) Then
                    transaction.Cancel
                End If
                Throw
            End Try
        End Sub

        Friend Sub OnStateMachineView(ByVal sender As Object, ByVal e As EventArgs)
            Me.SetLeafActiveDesigner(Me, Nothing)
        End Sub

        Friend Sub OnStatusAddEventDriven(ByVal sender As Object, ByVal e As EventArgs)
            Dim verb As ActivityDesignerVerb = [TryCast](sender,ActivityDesignerVerb)
            If (Not verb Is Nothing) Then
                Dim state As StateActivity = DirectCast(MyBase.Activity, StateActivity)
                Dim flag2 As Boolean = (StateMachineHelpers.IsLeafState(state) AndAlso StateMachineHelpers.IsCompletedState(state))
                Dim flag As Boolean = (((Me.GetIsEditable AndAlso Not Me.HasActiveDesigner) AndAlso (Not flag2 AndAlso Not MyBase.IsLocked)) AndAlso Not Me.IsStateCustomActivity)
                verb.Visible = flag
                verb.Enabled = flag
            End If
        End Sub

        Friend Sub OnStatusAddState(ByVal sender As Object, ByVal e As EventArgs)
            Dim verb As ActivityDesignerVerb = [TryCast](sender,ActivityDesignerVerb)
            If (Not verb Is Nothing) Then
                Dim state As StateActivity = DirectCast(MyBase.Activity, StateActivity)
                Dim flag2 As Boolean = False
                Dim flag3 As Boolean = False
                If StateMachineHelpers.IsLeafState(state) Then
                    flag2 = StateMachineHelpers.IsInitialState(state)
                    flag3 = StateMachineHelpers.IsCompletedState(state)
                End If
                Dim flag As Boolean = ((((Me.GetIsEditable AndAlso Not Me.HasActiveDesigner) AndAlso (Not flag2 AndAlso Not flag3)) AndAlso Not MyBase.IsLocked) AndAlso Not Me.IsStateCustomActivity)
                verb.Visible = flag
                verb.Enabled = flag
            End If
        End Sub

        Friend Sub OnStatusAddStateFinalization(ByVal sender As Object, ByVal e As EventArgs)
            Dim verb As ActivityDesignerVerb = [TryCast](sender,ActivityDesignerVerb)
            If (Not verb Is Nothing) Then
                Dim state As StateActivity = DirectCast(MyBase.Activity, StateActivity)
                Dim flag2 As Boolean = StateMachineHelpers.IsLeafState(state)
                Dim flag3 As Boolean = (flag2 AndAlso StateMachineHelpers.IsCompletedState(state))
                Dim flag4 As Boolean = (Me.DesignersParser.StateFinalizationDesigners.Count > 0)
                Dim flag As Boolean = ((((Me.GetIsEditable AndAlso Not Me.HasActiveDesigner) AndAlso (flag2 AndAlso Not flag3)) AndAlso (Not flag4 AndAlso Not MyBase.IsLocked)) AndAlso Not Me.IsStateCustomActivity)
                verb.Visible = flag
                verb.Enabled = flag
            End If
        End Sub

        Friend Sub OnStatusAddStateInitialization(ByVal sender As Object, ByVal e As EventArgs)
            Dim verb As ActivityDesignerVerb = [TryCast](sender,ActivityDesignerVerb)
            If (Not verb Is Nothing) Then
                Dim state As StateActivity = DirectCast(MyBase.Activity, StateActivity)
                Dim flag2 As Boolean = StateMachineHelpers.IsLeafState(state)
                Dim flag3 As Boolean = (flag2 AndAlso StateMachineHelpers.IsCompletedState(state))
                Dim flag4 As Boolean = (Me.DesignersParser.StateInitializationDesigners.Count > 0)
                Dim flag As Boolean = ((((Me.GetIsEditable AndAlso Not Me.HasActiveDesigner) AndAlso (flag2 AndAlso Not flag3)) AndAlso (Not flag4 AndAlso Not MyBase.IsLocked)) AndAlso Not Me.IsStateCustomActivity)
                verb.Visible = flag
                verb.Enabled = flag
            End If
        End Sub

        Friend Sub OnStatusSetAsCompletedState(ByVal sender As Object, ByVal e As EventArgs)
            Dim verb As ActivityDesignerVerb = [TryCast](sender,ActivityDesignerVerb)
            If (Not verb Is Nothing) Then
                Dim flag As Boolean = False
                If Not Me.HasActiveDesigner Then
                    Dim state As StateActivity = DirectCast(MyBase.Activity, StateActivity)
                    Dim rootState As StateActivity = StateMachineHelpers.GetRootState(state)
                    flag = (((StateMachineHelpers.IsLeafState(state) AndAlso StateMachineHelpers.IsStateMachine(rootState)) AndAlso Not StateMachineHelpers.IsCompletedState(state)) AndAlso (state.Activities.Count = 0))
                End If
                verb.Visible = flag
                verb.Enabled = flag
            End If
        End Sub

        Friend Sub OnStatusSetAsInitialState(ByVal sender As Object, ByVal e As EventArgs)
            Dim verb As ActivityDesignerVerb = [TryCast](sender,ActivityDesignerVerb)
            If (Not verb Is Nothing) Then
                Dim flag As Boolean = False
                If Not Me.HasActiveDesigner Then
                    Dim state As StateActivity = DirectCast(MyBase.Activity, StateActivity)
                    Dim rootState As StateActivity = StateMachineHelpers.GetRootState(state)
                    flag = ((StateMachineHelpers.IsLeafState(state) AndAlso StateMachineHelpers.IsStateMachine(rootState)) AndAlso Not StateMachineHelpers.IsInitialState(state))
                End If
                verb.Visible = flag
                verb.Enabled = flag
            End If
        End Sub

        Friend Sub OnStatusStateMachineView(ByVal sender As Object, ByVal e As EventArgs)
            Dim verb As ActivityDesignerVerb = [TryCast](sender,ActivityDesignerVerb)
            If (Not verb Is Nothing) Then
                Dim hasActiveDesigner As Boolean = Me.HasActiveDesigner
                verb.Visible = hasActiveDesigner
                verb.Enabled = hasActiveDesigner
            End If
        End Sub

        Protected Overrides Sub OnThemeChange(ByVal newTheme As ActivityDesignerTheme)
            MyBase.OnThemeChange(newTheme)
            Me.Image = StateDesigner.GetDesignerImage(Me)
        End Sub

        Private Sub RecalculateRootDesignerSize()
            Dim empty As Size = Size.Empty
            Dim designer As ActivityDesigner
            For Each designer In Me.ContainedDesigners
                If Me.IsContainedDesignerVisible(designer) Then
                    Dim bounds As Rectangle = designer.Bounds
                    bounds.Offset((StateDesigner.Separator.Width - Me.Location.X), (StateDesigner.Separator.Height - Me.Location.Y))
                    empty.Width = Math.Max(empty.Width, bounds.Right)
                    empty.Height = Math.Max(empty.Height, bounds.Bottom)
                End If
            Next
            empty.Width = Math.Max(empty.Width, Me.MinimumSize.Width)
            empty.Height = Math.Max(empty.Height, Me.MinimumSize.Height)
            Me.Size = empty
        End Sub

        Private Sub RefreshRootDesignerLayout()
            If Not Me.HasActiveDesigner Then
                Me._eventHandlersLayout.Layouts.Clear
                Me.DesignerLayouts.Clear
                Me._designersParser = New ContainedDesignersParser(Me.ContainedDesigners)
                Dim designer As StateInitializationDesigner
                For Each designer In Me.DesignersParser.StateInitializationDesigners
                    Dim item As New DesignerLayout(designer)
                    Me.DesignerLayouts.Item(designer.Activity) = item
                    Me._eventHandlersLayout.Layouts.Add(item)
                Next
                Dim designer2 As EventDrivenDesigner
                For Each designer2 In Me.DesignersParser.EventDrivenDesigners
                    Dim layout2 As New DesignerLayout(designer2)
                    Me.DesignerLayouts.Item(designer2.Activity) = layout2
                    Me._eventHandlersLayout.Layouts.Add(layout2)
                Next
                Dim designer3 As StateFinalizationDesigner
                For Each designer3 In Me.DesignersParser.StateFinalizationDesigners
                    Dim layout3 As New DesignerLayout(designer3)
                    Me.DesignerLayouts.Item(designer3.Activity) = layout3
                    Me._eventHandlersLayout.Layouts.Add(layout3)
                Next
                Me.RootDesignerLayout = Me._statesLayout
            Else
                Me.RootDesignerLayout = Me._eventDrivenLayout
            End If
        End Sub

        Private Sub RelocateStates()
            Dim num As Integer = (Me._eventHandlersLayout.Bounds.Bottom + &H10)
            Dim num2 As Integer = 0
            Dim empty As Rectangle = Rectangle.Empty
            Dim num3 As Integer = &H7FFFFFFF
            Dim designer As ActivityDesigner
            For Each designer In Me.ContainedDesigners
                If Not Me.IsContainedDesignerVisible(designer) Then
                    [Continue] For
                End If
                Dim designer2 As StateDesigner = [TryCast](designer,StateDesigner)
                If (Not designer2 Is Nothing) Then
                    If (designer2.Location.Y < num) Then
                        num2 = Math.Max(num2, (num - designer2.Location.Y))
                        If empty.IsEmpty Then
                            empty = designer2.Bounds
                        Else
                            empty = Rectangle.Union(empty, designer2.Bounds)
                        End If
                        [Continue] For
                    End If
                    num3 = Math.Min(num3, (designer2.Location.Y - num))
                End If
            Next
            If (num3 = &H7FFFFFFF) Then
                num3 = 0
            End If
            If (num2 > 0) Then
                Dim num4 As Integer = -2147483648
                Dim designer3 As ActivityDesigner
                For Each designer3 In Me.ContainedDesigners
                    If Not Me.IsContainedDesignerVisible(designer3) Then
                        [Continue] For
                    End If
                    Dim designer4 As StateDesigner = [TryCast](designer3,StateDesigner)
                    If (Not designer4 Is Nothing) Then
                        Dim location As Point = designer4.Location
                        If (designer4.Location.Y < num) Then
                            designer4.Location = New Point(location.X, (location.Y + num2))
                        Else
                            designer4.Location = New Point(location.X, (((location.Y + empty.Height) + &H10) - num3))
                        End If
                        num4 = Math.Max(num4, designer4.Bounds.Bottom)
                    End If
                Next
                If (num4 > MyBase.Bounds.Bottom) Then
                    Dim size As New Size(Me.Size.Width, (Me.Size.Height + ((num4 + &H10) - MyBase.Bounds.Bottom)))
                    Me.Size = size
                End If
            End If
        End Sub

        Private Sub RepositionStates()
            Dim num As Integer = (Me._eventHandlersLayout.Bounds.Bottom + &H10)
            Dim designer As ActivityDesigner
            For Each designer In Me.ContainedDesigners
                If Me.IsContainedDesignerVisible(designer) Then
                    Dim designer2 As StateDesigner = [TryCast](designer,StateDesigner)
                    If (Not designer2 Is Nothing) Then
                        Dim x As Integer = (Me.Location.X + &H10)
                        Dim y As Integer = num
                        designer2.Location = New Point(x, y)
                        num = (designer2.Bounds.Bottom + &H10)
                    End If
                End If
            Next
            Me.NeedsAutoLayout = False
        End Sub

        Private Sub SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
            If Me.HasActiveDesigner Then
                Dim activity As StateActivity = DirectCast(MyBase.Activity, StateActivity)
                Dim primarySelection As Activity = [TryCast](Me.SelectionService.PrimarySelection,Activity)
                If (((Not primarySelection Is Nothing) AndAlso activity.Activities.Contains(primarySelection)) AndAlso (Not Me.ActiveDesigner.Activity Is primarySelection)) Then
                    Dim designer As ActivityDesigner = StateDesigner.GetDesigner(primarySelection)
                    If Not TypeOf designer Is StateDesigner Then
                        Me.SetActiveDesigner(designer)
                    End If
                End If
            ElseIf (MyBase.Activity Is Me.SelectionService.PrimarySelection) Then
                MyBase.RefreshDesignerVerbs
            End If
        End Sub

        Private Sub SetActiveDesigner(ByVal designer As ActivityDesigner)
            Dim qualifiedName As String = Nothing
            If (designer Is Nothing) Then
                If Not Me.HasActiveDesigner Then
                    Return
                End If
            Else
                qualifiedName = designer.Activity.QualifiedName
                If (Me.HasActiveDesigner AndAlso (Me.ActiveDesigner.Activity.QualifiedName = qualifiedName)) Then
                    Return
                End If
            End If
            Dim service As IDesignerHost = [TryCast](MyBase.GetService(GetType(IDesignerHost)),IDesignerHost)
            Dim transaction As DesignerTransaction = Nothing
            If (Not service Is Nothing) Then
                transaction = service.CreateTransaction(SR.GetString("UndoSwitchViews"))
            End If
            Try 
                Dim rootStateDesigner As StateDesigner = Me.RootStateDesigner
                Me.SetLeafActiveDesigner(rootStateDesigner, Nothing)
                Me.SetActiveDesignerHelper(Me, designer)
                If (Not transaction Is Nothing) Then
                    transaction.Commit
                End If
            Catch obj1 As Object
                If (Not transaction Is Nothing) Then
                    transaction.Cancel
                End If
                Throw
            End Try
        End Sub

        Private Sub SetActiveDesignerByName(ByVal activeDesignerName As String)
            Dim designer As ActivityDesigner = Nothing
            If Not String.IsNullOrEmpty(activeDesignerName) Then
                Dim designer2 As ActivityDesigner
                For Each designer2 In Me.ContainedDesigners
                    If (designer2.Activity.QualifiedName = activeDesignerName) Then
                        designer = designer2
                        Exit For
                    End If
                Next
            End If
            Me.ActiveDesigner = designer
        End Sub

        Private Sub SetActiveDesignerHelper(ByVal stateDesigner As StateDesigner, ByVal activeDesigner As ActivityDesigner)
            Dim service As WorkflowDesignerLoader = [TryCast](MyBase.GetService(GetType(WorkflowDesignerLoader)),WorkflowDesignerLoader)
            If ((Not service Is Nothing) AndAlso service.InDebugMode) Then
                stateDesigner.ActiveDesigner = activeDesigner
            Else
                Dim propertyDescriptor As PropertyDescriptor = StateDesigner.GetPropertyDescriptor(stateDesigner.Activity, "ActiveDesignerName")
                If (activeDesigner Is Nothing) Then
                    propertyDescriptor.SetValue(stateDesigner.Activity, Nothing)
                Else
                    propertyDescriptor.SetValue(stateDesigner.Activity, activeDesigner.Activity.QualifiedName)
                End If
            End If
        End Sub

        Private Sub SetLeafActiveDesigner(ByVal parentDesigner As StateDesigner, ByVal activityDesigner As ActivityDesigner)
            Dim stateDesigner As StateDesigner = parentDesigner
            Do While True
                Dim activeDesigner As StateDesigner = [TryCast](stateDesigner.ActiveDesigner,StateDesigner)
                If (activeDesigner Is Nothing) Then
                    Exit Do
                End If
                stateDesigner = activeDesigner
            Loop
            Me.SetActiveDesignerHelper(stateDesigner, activityDesigner)
        End Sub

        Private Sub SetParentTreeEnsuringVisible(ByVal value As Boolean)
            Me._ensuringVisible = value
            Dim designer As StateDesigner = [TryCast](MyBase.ParentDesigner,StateDesigner)
            Do While (Not designer Is Nothing)
                designer._ensuringVisible = value
                designer = [TryCast](designer.ParentDesigner,StateDesigner)
            Loop
        End Sub

        Friend Sub StateDesignerLinkMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
            Dim host As IDesignerHost = [TryCast](MyBase.GetService(GetType(IDesignerHost)),IDesignerHost)
            Dim transaction As DesignerTransaction = Nothing
            If (Not host Is Nothing) Then
                transaction = host.CreateTransaction(SR.GetString("UndoSwitchViews"))
            End If
            Try 
                Dim service As ISelectionService = [TryCast](MyBase.GetService(GetType(ISelectionService)),ISelectionService)
                If (Not service Is Nothing) Then
                    service.SetSelectedComponents(New Object() { MyBase.Activity }, SelectionTypes.Click)
                End If
                Me.SetLeafActiveDesigner(Me, Nothing)
                If (Not transaction Is Nothing) Then
                    transaction.Commit
                End If
            Catch obj1 As Object
                If (Not transaction Is Nothing) Then
                    transaction.Cancel
                End If
                Throw
            End Try
        End Sub

        Private Sub UpdateConnectors()
            Try 
                Me.RootStateDesigner.RemovingSetState = False
                Dim rootState As StateActivity = DirectCast(MyBase.Activity, StateActivity)
                Dim onlys As ReadOnlyCollection([Of] TransitionInfo) = TransitionInfo.ParseStateMachine(rootState)
                Dim array As Connector() = New Connector(MyBase.Connectors.Count  - 1) {}
                MyBase.Connectors.CopyTo(array, 0)
                Dim connector As Connector
                For Each connector In array
                    Dim stateDesignerConnector As StateDesignerConnector = [TryCast](connector,StateDesignerConnector)
                    If (stateDesignerConnector Is Nothing) Then
                        MyBase.RemoveConnector(connector)
                    Else
                        Dim flag As Boolean = False
                        Dim info As TransitionInfo
                        For Each info In onlys
                            If info.Matches(stateDesignerConnector) Then
                                info.Connector = stateDesignerConnector
                                flag = True
                                Exit For
                            End If
                        Next
                        If Not flag Then
                            MyBase.RemoveConnector(connector)
                        End If
                    End If
                Next
                Dim info2 As TransitionInfo
                For Each info2 In onlys
                    If ((info2.Connector Is Nothing) AndAlso (Not info2.TargetState Is Nothing)) Then
                        Dim eventHandlerConnectionPoint As DesignerLayoutConnectionPoint = Me.GetEventHandlerConnectionPoint(info2.EventHandler)
                        Dim targetStateConnectionPoint As ConnectionPoint = Me.GetTargetStateConnectionPoint(info2.TargetState)
                        If ((Not eventHandlerConnectionPoint Is Nothing) AndAlso (Not targetStateConnectionPoint Is Nothing)) Then
                            Me.RootStateDesigner.AddingSetState = False
                            Try 
                                Dim connector3 As StateDesignerConnector = DirectCast(MyBase.AddConnector(eventHandlerConnectionPoint, targetStateConnectionPoint), StateDesignerConnector)
                                connector3.SetStateName = info2.SetState.QualifiedName
                                connector3.TargetStateName = info2.SetState.TargetStateName
                                If (Not info2.EventHandler Is Nothing) Then
                                    connector3.EventHandlerName = info2.EventHandler.QualifiedName
                                End If
                                [Continue] For
                            Finally
                                Me.RootStateDesigner.AddingSetState = True
                            End Try
                        End If
                    End If
                Next
            Finally
                Me.RemovingSetState = True
            End Try
        End Sub


        ' Properties
        Friend Property ActiveDesigner As ActivityDesigner
            Get
                Return Me._activeDesigner
            End Get
            Set(ByVal value As ActivityDesigner)
                If (Not Me._activeDesigner Is value) Then
                    Me._activeDesigner = value
                    MyBase.AutoSize = (value Is Nothing)
                    MyBase.RefreshDesignerVerbs
                    If Me.IsRootStateDesigner Then
                        MyBase.PerformLayout
                    Else
                        Dim parentDesigner As StateDesigner = [TryCast](MyBase.ParentDesigner,StateDesigner)
                        If (value Is Nothing) Then
                            Me.SetActiveDesignerHelper(parentDesigner, Nothing)
                        Else
                            Me.SetActiveDesignerHelper(parentDesigner, Me)
                        End If
                    End If
                    If (Me._activeDesigner Is Nothing) Then
                        Me._minimumSize = Me._stateMinimumSize
                        Me.Location = Me._stateLocation
                        Me.Size = Me._stateSize
                    Else
                        Me._stateLocation = Me.Location
                        Me._stateSize = Me.Size
                        Me._stateMinimumSize = Me._minimumSize
                        Me._minimumSize = Size.Empty
                    End If
                End If
            End Set
        End Property

        Friend Property AddingSetState As Boolean
            Get
                Return Me._addingSetState
            End Get
            Set(ByVal value As Boolean)
                Me._addingSetState = value
            End Set
        End Property

        Private ReadOnly Property BottomConnectionPoint As Point
            Get
                Dim bounds As Rectangle = MyBase.Bounds
                Return New Point((bounds.X + (bounds.Width / 2)), bounds.Bottom)
            End Get
        End Property

        Public Overrides ReadOnly Property CanExpandCollapse As Boolean
            Get
                Return False
            End Get
        End Property

        Friend Property Cursor As Cursor
            Get
                Return MyBase.ParentView.Cursor
            End Get
            Set(ByVal value As Cursor)
                MyBase.ParentView.Cursor = value
            End Set
        End Property

        Friend ReadOnly Property DesignerLayouts As Dictionary([Of] Activity, DesignerLayout)
            Get
                If (Me._designerLayouts Is Nothing) Then
                    Me._designerLayouts = New Dictionary([Of] Activity, DesignerLayout)
                End If
                Return Me._designerLayouts
            End Get
        End Property

        Private Property DesignersParser As ContainedDesignersParser
            Get
                If (Me._designersParser Is Nothing) Then
                    Me._designersParser = New ContainedDesignersParser(Me.ContainedDesigners)
                End If
                Return Me._designersParser
            End Get
            Set(ByVal value As ContainedDesignersParser)
                Me._designersParser = value
            End Set
        End Property

        Private Property DragDropActive As Boolean
            Get
                Return Me._dragDropActive
            End Get
            Set(ByVal value As Boolean)
                If (value <> Me._dragDropActive) Then
                    Me._dragDropActive = value
                    MyBase.Invalidate
                End If
            End Set
        End Property

        Friend ReadOnly Property EventHandlersBounds As ReadOnlyCollection([Of] Rectangle)
            Get
                Dim list As New List([Of] Rectangle)
                Dim layout As DesignerLayout
                For Each layout In Me.DesignerLayouts.Values
                    Dim bounds As Rectangle = layout.Bounds
                    bounds.Inflate(0, 4)
                    list.Add(bounds)
                Next
                Return list.AsReadOnly
            End Get
        End Property

        Protected Overrides ReadOnly Property ExpandButtonRectangle As Rectangle
            Get
                Return Rectangle.Empty
            End Get
        End Property

        Public Overrides ReadOnly Property FirstSelectableObject As Object
            Get
                If Me.IsVisible Then
                    If Me.HasActiveDesigner Then
                        Return Me.ActiveDesigner.Activity
                    End If
                    If (Me.DesignersParser.Ordered.Count > 0) Then
                        Return Me.DesignersParser.Ordered.Item(0).Activity
                    End If
                End If
                Return Nothing
            End Get
        End Property

        Protected Overrides ReadOnly Property Glyphs As ActivityDesignerGlyphCollection
            Get
                Dim glyphs As New ActivityDesignerGlyphCollection
                glyphs.AddRange(MyBase.Glyphs)
                If Not Me.HasActiveDesigner Then
                    Dim designer As EventDrivenDesigner
                    For Each designer In Me.DesignersParser.EventDrivenDesigners
                        Dim layout As Layout = Me.RootDesignerLayout.GetLayout(designer)
                        If (Not layout Is Nothing) Then
                            If designer.IsSelected Then
                                Dim item As New LayoutSelectionGlyph(layout)
                                glyphs.Add(item)
                            End If
                            If Not designer.Activity.Enabled Then
                                Dim glyph2 As New CommentLayoutGlyph(layout)
                                glyphs.Add(glyph2)
                            End If
                        End If
                    Next
                    Dim designer2 As StateInitializationDesigner
                    For Each designer2 In Me.DesignersParser.StateInitializationDesigners
                        Dim layout2 As Layout = Me.RootDesignerLayout.GetLayout(designer2)
                        If (Not layout2 Is Nothing) Then
                            If designer2.IsSelected Then
                                Dim glyph3 As New LayoutSelectionGlyph(layout2)
                                glyphs.Add(glyph3)
                            End If
                            If Not designer2.Activity.Enabled Then
                                Dim glyph4 As New CommentLayoutGlyph(layout2)
                                glyphs.Add(glyph4)
                            End If
                        End If
                    Next
                    Dim designer3 As StateFinalizationDesigner
                    For Each designer3 In Me.DesignersParser.StateFinalizationDesigners
                        Dim layout3 As Layout = Me.RootDesignerLayout.GetLayout(designer3)
                        If (Not layout3 Is Nothing) Then
                            If designer3.IsSelected Then
                                Dim glyph5 As New LayoutSelectionGlyph(layout3)
                                glyphs.Add(glyph5)
                            End If
                            If Not designer3.Activity.Enabled Then
                                Dim glyph6 As New CommentLayoutGlyph(layout3)
                                glyphs.Add(glyph6)
                            End If
                        End If
                    Next
                End If
                Return glyphs
            End Get
        End Property

        Friend ReadOnly Property HasActiveDesigner As Boolean
            Get
                Return (Not Me.ActiveDesigner Is Nothing)
            End Get
        End Property

        Friend Overridable ReadOnly Property HelpText As String
            Get
                If (Me._helpText Is Nothing) Then
                    Me._helpText = DR.GetString("StateHelpText")
                End If
                Return Me._helpText
            End Get
        End Property

        Property Image As Image
            Public Overrides Get
                Dim state As StateActivity = [TryCast](MyBase.Activity,StateActivity)
                If ((Not state Is Nothing) AndAlso StateMachineHelpers.IsLeafState(state)) Then
                    If StateMachineHelpers.IsInitialState(state) Then
                        If Not StateMachineHelpers.IsCompletedState(state) Then
                            Return StateDesigner.GetInitialStateDesignerImage(Me)
                        End If
                    ElseIf StateMachineHelpers.IsCompletedState(state) Then
                        Return StateDesigner.GetCompletedStateDesignerImage(Me)
                    End If
                End If
                Return MyBase.Image
            End Get
            Protected Overrides Set(ByVal value As Image)
                MyBase.Image = value
            End Set
        End Property

        Protected Overrides ReadOnly Property ImageRectangle As Rectangle
            Get
                If (Me.HasActiveDesigner AndAlso Not Me.IsRootStateDesigner) Then
                    Return New Rectangle(-1, -1, 1, 1)
                End If
                Return Me._titleBarLayout.ImageLayout.Bounds
            End Get
        End Property

        Private ReadOnly Property InlineLayout As DesignerLinkLayout
            Get
                Return Me._designerLinkLayout
            End Get
        End Property

        Friend ReadOnly Property IsRootStateDesigner As Boolean
            Get
                Return ((Not MyBase.Activity.Site Is Nothing) AndAlso StateMachineHelpers.IsRootState(DirectCast(MyBase.Activity, StateActivity)))
            End Get
        End Property

        Private ReadOnly Property IsStateCustomActivity As Boolean
            Get
                Dim state As StateActivity = DirectCast(MyBase.Activity, StateActivity)
                Return (Not StateMachineHelpers.IsStateMachine(state) AndAlso (state.Parent Is Nothing))
            End Get
        End Property

        Public Overrides ReadOnly Property LastSelectableObject As Object
            Get
                If Me.IsVisible Then
                    If Me.HasActiveDesigner Then
                        Return Me.ActiveDesigner.Activity
                    End If
                    If (Me.DesignersParser.Ordered.Count > 0) Then
                        Return Me.DesignersParser.Ordered.Item((Me.DesignersParser.Ordered.Count - 1)).Activity
                    End If
                End If
                Return Nothing
            End Get
        End Property

        Public Overrides Property Location As Point
            Get
                Return MyBase.Location
            End Get
            Set(ByVal value As Point)
                If (MyBase.Location <> value) Then
                    If ((Me.HasActiveDesigner AndAlso Not Me.PerformingLayout) AndAlso Not Me.IsRootStateDesigner) Then
                        Me._stateLocation = value
                    Else
                        If Me.IsRootStateDesigner Then
                            Dim performingLayout As Boolean = Me.PerformingLayout
                            Me.PerformingLayout = True
                            Try 
                                MyBase.Location = value
                            Finally
                                Me.PerformingLayout = performingLayout
                            End Try
                        Else
                            MyBase.Location = value
                        End If
                        Me.RootDesignerLayout.MoveLayout(MyBase.Location)
                        MyBase.Invalidate
                    End If
                End If
            End Set
        End Property

        Public Overrides ReadOnly Property MinimumSize As Size
            Get
                Return Me._minimumSize
            End Get
        End Property

        Private Property NeedsAutoLayout As Boolean
            Get
                Return Me._needsAutoLayout
            End Get
            Set(ByVal value As Boolean)
                Me._needsAutoLayout = value
            End Set
        End Property

        Friend Property PerformingLayout As Boolean
            Get
                Return Me.RootStateDesigner._performingLayout
            End Get
            Set(ByVal value As Boolean)
                Me._performingLayout = value
            End Set
        End Property

        Friend Property RemovingSetState As Boolean
            Get
                Return Me._removingSetState
            End Get
            Set(ByVal value As Boolean)
                Me._removingSetState = value
            End Set
        End Property

        Private Property RootDesignerLayout As Layout
            Get
                If (Me._rootDesignerLayout Is Nothing) Then
                    Me.RefreshRootDesignerLayout
                End If
                Return Me._rootDesignerLayout
            End Get
            Set(ByVal value As Layout)
                Me._rootDesignerLayout = value
            End Set
        End Property

        Friend ReadOnly Property RootStateDesigner As StateDesigner
            Get
                If (Me._rootStateDesigner Is Nothing) Then
                    Dim rootState As StateActivity = StateMachineHelpers.GetRootState(DirectCast(MyBase.Activity, StateActivity))
                    Me._rootStateDesigner = [TryCast](StateDesigner.GetDesigner(rootState),StateDesigner)
                End If
                Return Me._rootStateDesigner
            End Get
        End Property

        Friend ReadOnly Property SelectionService As ISelectionService
            Get
                If (Me._selectionService Is Nothing) Then
                    Me._selectionService = DirectCast(MyBase.GetService(GetType(ISelectionService)), ISelectionService)
                    AddHandler Me._selectionService.SelectionChanged, New EventHandler(AddressOf Me.SelectionChanged)
                End If
                Return Me._selectionService
            End Get
        End Property

        Protected Overrides ReadOnly Property ShowConnectorsInForeground As Boolean
            Get
                Return True
            End Get
        End Property

        Public Overrides Property Size As Size
            Get
                Return MyBase.Size
            End Get
            Set(ByVal value As Size)
                If ((Me.HasActiveDesigner AndAlso Not Me.PerformingLayout) AndAlso Not Me.IsRootStateDesigner) Then
                    Me._stateSize = value
                Else
                    If Me.IsRootStateDesigner Then
                        Dim performingLayout As Boolean = Me.PerformingLayout
                        Me.PerformingLayout = True
                        Try 
                            MyBase.Size = value
                        Finally
                            Me.PerformingLayout = performingLayout
                        End Try
                    Else
                        MyBase.Size = value
                    End If
                    Dim newSize As Size = MyBase.Size
                    Me.RootDesignerLayout.ResizeLayout(newSize)
                End If
            End Set
        End Property

        Public Overrides ReadOnly Property [Text] As String
            Get
                Dim text As String = MyBase.Text
                If String.IsNullOrEmpty([text]) Then
                    [text] = MyBase.Activity.GetType.Name
                End If
                Return [text]
            End Get
        End Property

        Protected Overrides ReadOnly Property TextRectangle As Rectangle
            Get
                If (Me.HasActiveDesigner AndAlso Not Me.IsRootStateDesigner) Then
                    Return Rectangle.Empty
                End If
                Return Me._titleBarLayout.TextLayout.Bounds
            End Get
        End Property

        Private ReadOnly Property TopConnectionPoint As Point
            Get
                Dim bounds As Rectangle = MyBase.Bounds
                Return New Point((bounds.X + (bounds.Width / 2)), bounds.Top)
            End Get
        End Property

        Friend Overridable ReadOnly Property ValidChildTypes As ReadOnlyCollection([Of] Type)
            Get
                Dim list As New List([Of] Type) { GetType(StateActivity), GetType(EventDrivenActivity) }
                Dim state As StateActivity = DirectCast(MyBase.Activity, StateActivity)
                If StateMachineHelpers.IsLeafState(state) Then
                    If (Me.DesignersParser.StateInitializationDesigners.Count = 0) Then
                        list.Add(GetType(StateInitializationActivity))
                    End If
                    If (Me.DesignersParser.StateFinalizationDesigners.Count = 0) Then
                        list.Add(GetType(StateFinalizationActivity))
                    End If
                End If
                Return list.AsReadOnly
            End Get
        End Property

        Protected Overrides ReadOnly Property Verbs As ActivityDesignerVerbCollection
            Get
                Dim verbs As New ActivityDesignerVerbCollection
                verbs.AddRange(MyBase.Verbs)
                If (Me._verbs Is Nothing) Then
                    Me._verbs = New ActivityDesignerVerbCollection
                    Dim verb As New ActivityDesignerVerb(Me, DesignerVerbGroup.General, DR.GetString("StateMachineView"), New EventHandler(AddressOf Me.OnStateMachineView), New EventHandler(AddressOf Me.OnStatusStateMachineView))
                    Me._verbs.Add(verb)
                    Dim verb2 As New ActivityDesignerVerb(Me, DesignerVerbGroup.General, DR.GetString("SetAsInitialState"), New EventHandler(AddressOf Me.OnSetAsInitialState), New EventHandler(AddressOf Me.OnStatusSetAsInitialState))
                    Me._verbs.Add(verb2)
                    Dim verb3 As New ActivityDesignerVerb(Me, DesignerVerbGroup.General, DR.GetString("SetAsCompletedState"), New EventHandler(AddressOf Me.OnSetAsCompletedState), New EventHandler(AddressOf Me.OnStatusSetAsCompletedState))
                    Me._verbs.Add(verb3)
                    Dim verb4 As New ActivityDesignerVerb(Me, DesignerVerbGroup.General, DR.GetString("AddState"), New EventHandler(AddressOf Me.OnAddState), New EventHandler(AddressOf Me.OnStatusAddState))
                    Me._verbs.Add(verb4)
                    Dim verb5 As New ActivityDesignerVerb(Me, DesignerVerbGroup.General, DR.GetString("AddEventDriven"), New EventHandler(AddressOf Me.OnAddEventDriven), New EventHandler(AddressOf Me.OnStatusAddEventDriven))
                    Me._verbs.Add(verb5)
                    Dim verb6 As New ActivityDesignerVerb(Me, DesignerVerbGroup.General, DR.GetString("AddStateInitialization"), New EventHandler(AddressOf Me.OnAddStateInitialization), New EventHandler(AddressOf Me.OnStatusAddStateInitialization))
                    Me._verbs.Add(verb6)
                    Dim verb7 As New ActivityDesignerVerb(Me, DesignerVerbGroup.General, DR.GetString("AddStateFinalization"), New EventHandler(AddressOf Me.OnAddStateFinalization), New EventHandler(AddressOf Me.OnStatusAddStateFinalization))
                    Me._verbs.Add(verb7)
                End If
                verbs.AddRange(Me._verbs)
                Return verbs
            End Get
        End Property


        ' Fields
        Private _activeDesigner As ActivityDesigner
        Private _addingSetState As Boolean = True
        Private _designerLayouts As Dictionary([Of] Activity, DesignerLayout)
        Private _designerLinkLayout As DesignerLinkLayout
        Private _designersParser As ContainedDesignersParser
        Private _dragDropActive As Boolean
        Friend _ensuringVisible As Boolean
        Private _eventDrivenLayout As EventDrivenLayout
        Private _eventHandlersLayout As EventHandlersLayout
        Private _helpText As String
        Private _minimumSize As Size = Size.Empty
        Private _needsAutoLayout As Boolean
        Private _performingLayout As Boolean
        Private _removingSetState As Boolean = True
        Private _rootDesignerLayout As Layout
        Private _rootStateDesigner As StateDesigner
        Private _selectionService As ISelectionService
        Private _stateLocation As Point
        Private _stateMinimumSize As Size
        Private _stateSize As Size
        Private _statesLayout As StatesLayout
        Private _titleBarLayout As TitleBarLayout
        Private _verbs As ActivityDesignerVerbCollection
        Private Const ActiveDesignerNamePropertyName As String = "ActiveDesignerName"
        Friend Shared ReadOnly CompletedState As Image = DR.GetImage("CompletedState")
        Private Const DefaultStateDesignerAutoLayoutDistance As Integer = &H10
        Friend Shared ReadOnly InitialState As Image = DR.GetImage("InitialState")
        Friend Shared Separator As Size = New Size(30, 30)

        ' Nested Types
        Private Class BreadCrumbBarLayout
            Inherits Layout
            ' Methods
            Public Sub New(ByVal activityDesigner As ActivityDesigner)
                MyBase.New(activityDesigner)
            End Sub

            Private Sub InitializeLayouts()
                MyBase.Layouts.Clear
                Dim activityDesigner As StateDesigner = DirectCast(MyBase.ActivityDesigner, StateDesigner)
                Dim designer2 As StateDesigner = activityDesigner
                Do While (Not designer2 Is Nothing)
                    designer2.InlineLayout.ParentStateDesigner = activityDesigner
                    MyBase.Layouts.Add(designer2.InlineLayout)
                    designer2 = [TryCast](designer2.ActiveDesigner,StateDesigner)
                Loop
            End Sub

            Public Overrides Sub OnLayoutPosition(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                Dim location As Point = MyBase.Location
                Dim layout As Layout
                For Each layout In MyBase.Layouts
                    layout.Location = location
                    layout.OnLayoutPosition(graphics, designerTheme, ambientTheme)
                    location.X = (location.X + (layout.Size.Width + Me._breadCrumbSeparatorSize.Width))
                Next
            End Sub

            Public Overrides Sub OnLayoutSize(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme, ByVal containerSize As Size)
                MyBase.OnLayoutSize(graphics, designerTheme, ambientTheme, containerSize)
                Me.InitializeLayouts
                Dim theme As CompositeDesignerTheme = [TryCast](designerTheme,CompositeDesignerTheme)
                If (Not theme Is Nothing) Then
                    Dim boldFont As Font = designerTheme.BoldFont
                    Me._breadCrumbSeparatorSize = StateMachineDesignerPaint.MeasureString(graphics, boldFont, " : ", StringAlignment.Near, Size.Empty)
                    Dim empty As Size = Size.Empty
                    Dim layout As Layout
                    For Each layout In MyBase.Layouts
                        layout.OnLayoutSize(graphics, theme, ambientTheme, empty)
                        empty.Width = (empty.Width + (layout.Size.Width + Me._breadCrumbSeparatorSize.Width))
                        empty.Height = Math.Max(empty.Height, layout.Size.Height)
                    Next
                    Me.MinimumSize = empty
                    MyBase.Size = empty
                End If
            End Sub

            Public Overrides Sub OnPaint(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                If (MyBase.Layouts.Count <> 0) Then
                    Dim boldFont As Font = designerTheme.BoldFont
                    Dim textQuality As TextQuality = ambientTheme.TextQuality
                    Dim foregroundBrush As Brush = designerTheme.ForegroundBrush
                    Dim layouts As List([Of] Layout) = MyBase.Layouts
                    Dim i As Integer
                    For i = 0 To (layouts.Count - 1) - 1
                        Dim layout As Layout = layouts.Item(i)
                        layout.OnPaint(graphics, designerTheme, ambientTheme)
                        Dim boundingRect As New Rectangle(layout.Bounds.Right, layout.Location.Y, Me._breadCrumbSeparatorSize.Width, Me._breadCrumbSeparatorSize.Height)
                        ActivityDesignerPaint.DrawText(graphics, boldFont, " : ", boundingRect, StringAlignment.Near, textQuality, foregroundBrush)
                    Next i
                    layouts.Item((layouts.Count - 1)).OnPaint(graphics, designerTheme, ambientTheme)
                End If
            End Sub


            ' Fields
            Private _breadCrumbSeparatorSize As Size
            Private Const BreadCrumbSeparator As String = " : "
        End Class

        Private Class CommentLayoutGlyph
            Inherits DesignerGlyph
            ' Methods
            Public Sub New(ByVal layout As Layout)
                If (layout Is Nothing) Then
                    Throw New ArgumentNullException("layout")
                End If
                Me._layout = layout
            End Sub

            Public Overrides Function GetBounds(ByVal designer As ActivityDesigner, ByVal activated As Boolean) As Rectangle
                If (designer Is Nothing) Then
                    Throw New ArgumentNullException("designer")
                End If
                Return Me._layout.Bounds
            End Function

            Protected Overrides Sub OnPaint(ByVal graphics As Graphics, ByVal activated As Boolean, ByVal ambientTheme As AmbientTheme, ByVal designer As ActivityDesigner)
                If (designer Is Nothing) Then
                    Throw New ArgumentNullException("designer")
                End If
                If (graphics Is Nothing) Then
                    Throw New ArgumentNullException("graphics")
                End If
                Dim bounds As Rectangle = Me.GetBounds(designer, False)
                graphics.FillRectangle(StateMachineDesignerPaint.FadeBrush, bounds)
                graphics.FillRectangle(ambientTheme.CommentIndicatorBrush, bounds)
                graphics.DrawRectangle(ambientTheme.CommentIndicatorPen, bounds)
            End Sub


            ' Properties
            Public Overrides ReadOnly Property Priority As Integer
                Get
                    Return &H2710
                End Get
            End Property


            ' Fields
            Private _layout As Layout
        End Class

        Private Class ContainedDesignersParser
            ' Methods
            Friend Sub New(ByVal containedDesigners As ReadOnlyCollection([Of] ActivityDesigner))
                Dim designer As ActivityDesigner
                For Each designer In containedDesigners
                    Dim item As StateInitializationDesigner = [TryCast](designer,StateInitializationDesigner)
                    If (Not item Is Nothing) Then
                        Me._stateInitializationDesigners.Add(item)
                    Else
                        Dim designer3 As StateFinalizationDesigner = [TryCast](designer,StateFinalizationDesigner)
                        If (Not designer3 Is Nothing) Then
                            Me._stateFinalizationDesigners.Add(designer3)
                            [Continue] For
                        End If
                        Dim designer4 As EventDrivenDesigner = [TryCast](designer,EventDrivenDesigner)
                        If (Not designer4 Is Nothing) Then
                            Me._eventDrivenDesigners.Add(designer4)
                            [Continue] For
                        End If
                        Dim designer5 As StateDesigner = [TryCast](designer,StateDesigner)
                        If (Not designer5 Is Nothing) Then
                            If StateMachineHelpers.IsLeafState(DirectCast(designer.Activity, StateActivity)) Then
                                Me._leafStateDesigners.Add(designer5)
                                [Continue] For
                            End If
                            Me._stateDesigners.Add(designer5)
                        End If
                    End If
                Next
            End Sub


            ' Properties
            Public ReadOnly Property EventDrivenDesigners As List([Of] EventDrivenDesigner)
                Get
                    Return Me._eventDrivenDesigners
                End Get
            End Property

            Public ReadOnly Property LeafStateDesigners As List([Of] StateDesigner)
                Get
                    Return Me._leafStateDesigners
                End Get
            End Property

            Public ReadOnly Property Ordered As List([Of] ActivityDesigner)
                Get
                    If (Me._ordered Is Nothing) Then
                        Me._ordered = New List([Of] ActivityDesigner)
                        Me._ordered.AddRange(Me._stateInitializationDesigners.ToArray)
                        Me._ordered.AddRange(Me._eventDrivenDesigners.ToArray)
                        Me._ordered.AddRange(Me._stateFinalizationDesigners.ToArray)
                        Me._ordered.AddRange(Me._leafStateDesigners.ToArray)
                        Me._ordered.AddRange(Me._stateDesigners.ToArray)
                    End If
                    Return Me._ordered
                End Get
            End Property

            Public ReadOnly Property StateDesigners As List([Of] StateDesigner)
                Get
                    Return Me._stateDesigners
                End Get
            End Property

            Public ReadOnly Property StateFinalizationDesigners As List([Of] StateFinalizationDesigner)
                Get
                    Return Me._stateFinalizationDesigners
                End Get
            End Property

            Public ReadOnly Property StateInitializationDesigners As List([Of] StateInitializationDesigner)
                Get
                    Return Me._stateInitializationDesigners
                End Get
            End Property


            ' Fields
            Private _eventDrivenDesigners As List([Of] EventDrivenDesigner) = New List([Of] EventDrivenDesigner)
            Private _leafStateDesigners As List([Of] StateDesigner) = New List([Of] StateDesigner)
            Private _ordered As List([Of] ActivityDesigner)
            Private _stateDesigners As List([Of] StateDesigner) = New List([Of] StateDesigner)
            Private _stateFinalizationDesigners As List([Of] StateFinalizationDesigner) = New List([Of] StateFinalizationDesigner)
            Private _stateInitializationDesigners As List([Of] StateInitializationDesigner) = New List([Of] StateInitializationDesigner)
        End Class

        Friend Class DesignerLayout
            Inherits DesignerLayoutBase
            ' Methods
            Public Sub New(ByVal activityDesigner As ActivityDesigner)
                MyBase.New(activityDesigner)
            End Sub

            Public Overrides Function HitTest(ByVal point As Point) As HitTestInfo
                Dim nowhere As HitTestInfo = HitTestInfo.Nowhere
                If MyBase.Bounds.Contains(point) Then
                    nowhere = New HitTestInfo(MyBase.ActivityDesigner, (HitTestLocations.ActionArea Or HitTestLocations.Designer))
                End If
                Return nowhere
            End Function


            ' Properties
            Public Overridable ReadOnly Property LeftConnectionPoint As Point
                Get
                    Dim bounds As Rectangle = MyBase.Bounds
                    Return New Point((bounds.Left - 4), (bounds.Y + (bounds.Height / 2)))
                End Get
            End Property

            Public Overridable ReadOnly Property RightConnectionPoint As Point
                Get
                    Dim bounds As Rectangle = MyBase.Bounds
                    Return New Point((bounds.Right + 4), (bounds.Y + (bounds.Height / 2)))
                End Get
            End Property

        End Class

        Friend Class DesignerLayoutBase
            Inherits Layout
            ' Methods
            Public Sub New(ByVal activityDesigner As ActivityDesigner)
                MyBase.New(activityDesigner)
            End Sub

            Public Overrides Sub MoveLayout(ByVal newLocation As Point)
                Dim point As New Point((MyBase.Location.X - newLocation.X), (MyBase.Location.Y - newLocation.Y))
                Me._textLocation = New Point((Me._textLocation.X - point.X), (Me._textLocation.Y - point.Y))
                Me._imageLocation = New Point((Me._imageLocation.X - point.X), (Me._imageLocation.Y - point.Y))
                MyBase.MoveLayout(newLocation)
            End Sub

            Public Overrides Sub OnLayoutPosition(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                MyBase.OnLayoutPosition(graphics, designerTheme, ambientTheme)
                Dim location As Point = MyBase.Location
                location.X = (location.X + ambientTheme.Margin.Width)
                location.Y = (location.Y + (ambientTheme.Margin.Height / 2))
                Me._imageLocation = location
                location.X = (location.X + (Me._imageSize.Width + 4))
                Me._textLocation = location
            End Sub

            Public Overrides Sub OnLayoutSize(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme, ByVal containerSize As Size)
                MyBase.OnLayoutSize(graphics, designerTheme, ambientTheme, containerSize)
                Me._imageSize = designerTheme.ImageSize
                Dim text As String = Me.Text
                Dim font As Font = designerTheme.Font
                Me._textSize = StateMachineDesignerPaint.MeasureString(graphics, font, [text], StringAlignment.Near, Size.Empty)
                Dim width As Integer = ((Me._imageSize.Width + 4) + Me._textSize.Width)
                width = (width + (ambientTheme.Margin.Width * 2))
                Dim height As Integer = (Math.Max(Me._imageSize.Height, Me._textSize.Height) + ambientTheme.Margin.Height)
                Dim size As New Size(width, height)
                Me.MinimumSize = size
                MyBase.Size = size
            End Sub

            Public Overrides Sub OnPaint(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                Dim activityDesigner As ActivityDesigner = MyBase.ActivityDesigner
                Dim font As Font = designerTheme.Font
                Dim designerImage As Image = StateDesigner.GetDesignerImage(activityDesigner)
                If (Not designerImage Is Nothing) Then
                    ActivityDesignerPaint.DrawImage(graphics, designerImage, Me.ImageRectangle, DesignerContentAlignment.Fill)
                End If
                ActivityDesignerPaint.DrawText(graphics, font, Me.Text, Me.TextRectangle, StringAlignment.Near, ambientTheme.TextQuality, designerTheme.ForegroundBrush)
            End Sub


            ' Properties
            Public ReadOnly Property ImageRectangle As Rectangle
                Get
                    Dim rectangle As New Rectangle(Me._imageLocation, Me._imageSize)
                    Return StateMachineDesignerPaint.TrimRectangle(rectangle, MyBase.Bounds)
                End Get
            End Property

            Public ReadOnly Property [Text] As String
                Get
                    Return MyBase.ActivityDesigner.Activity.Name
                End Get
            End Property

            Public ReadOnly Property TextRectangle As Rectangle
                Get
                    Dim rectangle As New Rectangle(Me._textLocation, Me._textSize)
                    Return StateMachineDesignerPaint.TrimRectangle(rectangle, MyBase.Bounds)
                End Get
            End Property


            ' Fields
            Private _imageLocation As Point
            Private _imageSize As Size
            Private _textLocation As Point
            Private _textSize As Size
            Public Const ImagePadding As Integer = 4
        End Class

        Friend Class DesignerLayoutConnectionPoint
            Inherits ConnectionPoint
            ' Methods
            Public Sub New(ByVal associatedDesigner As ActivityDesigner, ByVal connectionIndex As Integer, ByVal eventHandler As CompositeActivity, ByVal designerEdges As DesignerEdges)
                MyBase.New(associatedDesigner, designerEdges, connectionIndex)
                Me._eventHandler = eventHandler
                Me._designerEdges = designerEdges
            End Sub


            ' Properties
            Public ReadOnly Property DesignerEdges As DesignerEdges
                Get
                    Return Me._designerEdges
                End Get
            End Property

            Public ReadOnly Property DesignerLayout As DesignerLayout
                Get
                    Dim layout As DesignerLayout
                    DirectCast(MyBase.AssociatedDesigner, StateDesigner).DesignerLayouts.TryGetValue(Me._eventHandler, layout)
                    Return layout
                End Get
            End Property

            Public ReadOnly Property EventHandler As CompositeActivity
                Get
                    Return Me._eventHandler
                End Get
            End Property

            Public Overrides ReadOnly Property Location As Point
                Get
                    Dim designerLayout As DesignerLayout = Me.DesignerLayout
                    If (designerLayout Is Nothing) Then
                        Return Point.Empty
                    End If
                    If (Me.DesignerEdges = DesignerEdges.Left) Then
                        Return designerLayout.LeftConnectionPoint
                    End If
                    Return designerLayout.RightConnectionPoint
                End Get
            End Property


            ' Fields
            Private _designerEdges As DesignerEdges
            Private _eventHandler As CompositeActivity
        End Class

        Private Class DesignerLinkLayout
            Inherits DesignerLayoutBase
            ' Methods
            Public Sub New(ByVal activityDesigner As ActivityDesigner)
                MyBase.New(activityDesigner)
            End Sub

            Public Overrides Sub OnMouseEnter()
                MyBase.OnMouseEnter
                If (Not Me.ParentStateDesigner Is Nothing) Then
                    Me._previousCursor = Me.ParentStateDesigner.Cursor
                    Me.ParentStateDesigner.Cursor = Cursors.Hand
                End If
                Me.MouseOver = True
            End Sub

            Public Overrides Sub OnMouseLeave()
                MyBase.OnMouseLeave
                If (Not Me.ParentStateDesigner Is Nothing) Then
                    Me.ParentStateDesigner.Cursor = Me._previousCursor
                End If
                Me.MouseOver = False
                Me.Invalidate
            End Sub

            Public Overrides Sub OnPaint(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                Dim activityDesigner As ActivityDesigner = MyBase.ActivityDesigner
                If Me.MouseOver Then
                    [Using] font As Font = New Font(designerTheme.Font, (FontStyle.Underline Or designerTheme.Font.Style))
                        Dim designerImage As Image = StateDesigner.GetDesignerImage(activityDesigner)
                        If (Not designerImage Is Nothing) Then
                            ActivityDesignerPaint.DrawImage(graphics, designerImage, MyBase.ImageRectangle, DesignerContentAlignment.Fill)
                        End If
                        ActivityDesignerPaint.DrawText(graphics, font, MyBase.Text, MyBase.TextRectangle, StringAlignment.Near, ambientTheme.TextQuality, designerTheme.ForegroundBrush)
                        Return
                    End [Using]
                End If
                MyBase.OnPaint(graphics, designerTheme, ambientTheme)
            End Sub


            ' Properties
            Public Property MouseOver As Boolean
                Get
                    Return Me._mouseOver
                End Get
                Set(ByVal value As Boolean)
                    If (value <> Me._mouseOver) Then
                        Me._mouseOver = value
                        Me.Invalidate
                    End If
                End Set
            End Property

            Public Property ParentStateDesigner As StateDesigner
                Get
                    Return Me._parentStateDesigner
                End Get
                Set(ByVal value As StateDesigner)
                    Me._parentStateDesigner = value
                End Set
            End Property


            ' Fields
            Private _mouseOver As Boolean
            Private _parentStateDesigner As StateDesigner
            Private _previousCursor As Cursor
        End Class

        Private Class EventDrivenLayout
            Inherits Layout
            ' Methods
            Public Sub New(ByVal activityDesigner As ActivityDesigner, ByVal titleBarLayout As TitleBarLayout)
                MyBase.New(activityDesigner)
                Me._breadCrumbBarLayout = New BreadCrumbBarLayout(activityDesigner)
                Me._designerLayout = New DesignerLinkLayout(activityDesigner)
                Dim designer As StateDesigner = [TryCast](activityDesigner,StateDesigner)
                If (Not designer Is Nothing) Then
                    Me._designerLayout.ParentStateDesigner = designer
                    AddHandler Me._designerLayout.MouseDown, New MouseEventHandler(AddressOf designer.StateDesignerLinkMouseDown)
                End If
                Me._titleBarLayout = titleBarLayout
            End Sub

            Private Sub InitializeLayout()
                MyBase.Layouts.Clear
                If Me.StateDesigner.IsRootStateDesigner Then
                    MyBase.Layouts.Add(Me._titleBarLayout)
                    MyBase.Layouts.Add(Me._breadCrumbBarLayout)
                Else
                    MyBase.Layouts.Add(Me._designerLayout)
                End If
            End Sub

            Public Overrides Sub OnLayoutPosition(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                If TypeOf designerTheme Is CompositeDesignerTheme Then
                    Dim bounds As Rectangle = MyBase.Bounds
                    Dim location As Point = bounds.Location
                    If Me.StateDesigner.IsRootStateDesigner Then
                        Me._titleBarLayout.Location = location
                        Me._titleBarLayout.OnLayoutPosition(graphics, designerTheme, ambientTheme)
                        location.X = (location.X + &H10)
                        location.Y = (location.Y + (Me._titleBarLayout.Size.Height + &H10))
                        Me._breadCrumbBarLayout.Location = location
                        Me._breadCrumbBarLayout.OnLayoutPosition(graphics, designerTheme, ambientTheme)
                        location.Y = (location.Y + (Me._breadCrumbBarLayout.Size.Height + &H10))
                    Else
                        Dim point2 As New Point((bounds.Left + ((bounds.Width - Me._designerLayout.Size.Width) / 2)), (bounds.Top + ambientTheme.SelectionSize.Height))
                        Me._designerLayout.Location = point2
                        Me._designerLayout.OnLayoutPosition(graphics, designerTheme, ambientTheme)
                        location.Y = ((Me._designerLayout.Bounds.Bottom + ambientTheme.SelectionSize.Height) + &H10)
                    End If
                    Dim size As Size = Me.StateDesigner.ActiveDesigner.Size
                    location.X = (bounds.Left + ((bounds.Width - size.Width) / 2))
                    Me.StateDesigner.ActiveDesigner.Location = location
                End If
            End Sub

            Public Overrides Sub OnLayoutSize(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme, ByVal containerSize As Size)
                MyBase.OnLayoutSize(graphics, designerTheme, ambientTheme, containerSize)
                Me.InitializeLayout
                Dim size As Size = containerSize
                Dim minimumSize As Size = Me.StateDesigner.MinimumSize
                size.Width = Math.Max(size.Width, minimumSize.Width)
                size.Height = Math.Max(size.Height, minimumSize.Height)
                Dim activeDesigner As ActivityDesigner = Me.StateDesigner.ActiveDesigner
                Dim size3 As Size = activeDesigner.Size
                If Me.StateDesigner.IsRootStateDesigner Then
                    Me._titleBarLayout.OnLayoutSize(graphics, designerTheme, ambientTheme, size)
                    Me._breadCrumbBarLayout.OnLayoutSize(graphics, designerTheme, ambientTheme, size)
                    size.Width = Math.Max(size.Width, (size3.Width + &H20))
                    size.Width = Math.Max(size.Width, Me._titleBarLayout.Size.Width)
                    size.Width = Math.Max(size.Width, Me._breadCrumbBarLayout.Size.Width)
                    Dim num As Integer = ((((size3.Height + Me._titleBarLayout.Size.Height) + Me._breadCrumbBarLayout.Size.Height) + &H30) + (ambientTheme.SelectionSize.Height * 2))
                    size.Height = Math.Max(size.Height, num)
                    Me._titleBarLayout.ResizeLayout(New Size(size.Width, Me._titleBarLayout.Size.Height))
                Else
                    Me._designerLayout.OnLayoutSize(graphics, designerTheme, ambientTheme, size)
                    size.Width = Math.Max(size.Width, (activeDesigner.Size.Width + &H20))
                    size.Width = Math.Max(size.Width, Me._designerLayout.Size.Width)
                    size.Height = Math.Max(size.Height, (((activeDesigner.Size.Height + &H20) + Me._designerLayout.Size.Height) + (ambientTheme.SelectionSize.Height * 2)))
                End If
                Me.MinimumSize = size
                MyBase.Size = size
            End Sub

            Public Overrides Sub OnPaint(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                Dim path As GraphicsPath = StateMachineDesignerPaint.GetDesignerPath(MyBase.ActivityDesigner, MyBase.Bounds, designerTheme)
                Dim backgroundBrush As Brush = designerTheme.GetBackgroundBrush(MyBase.Bounds)
                graphics.FillPath(backgroundBrush, path)
                MyBase.OnPaint(graphics, designerTheme, ambientTheme)
                If ambientTheme.ShowDesignerBorder Then
                    graphics.DrawPath(designerTheme.BorderPen, path)
                End If
            End Sub

            Public Overrides Sub ResizeLayout(ByVal newSize As Size)
                If Me.StateDesigner.IsRootStateDesigner Then
                    Me._titleBarLayout.ResizeLayout(New Size(newSize.Width, Me._titleBarLayout.Size.Height))
                End If
                MyBase.ResizeLayout(newSize)
            End Sub


            ' Properties
            Private ReadOnly Property StateDesigner As StateDesigner
                Get
                    Return DirectCast(MyBase.ActivityDesigner, StateDesigner)
                End Get
            End Property


            ' Fields
            Private _breadCrumbBarLayout As BreadCrumbBarLayout
            Private _designerLayout As DesignerLinkLayout
            Private _titleBarLayout As TitleBarLayout
            Private Const ActiveDesignerPadding As Integer = &H10
        End Class

        Private Class EventHandlersLayout
            Inherits Layout
            ' Methods
            Public Sub New(ByVal activityDesigner As ActivityDesigner)
                MyBase.New(activityDesigner)
            End Sub

            Public Overrides Sub OnLayoutPosition(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                Dim selectionSize As Size = ambientTheme.SelectionSize
                Dim x As Integer = (MyBase.Location.X + 8)
                Dim y As Integer = (MyBase.Location.Y + 8)
                Dim layout As Layout
                For Each layout In MyBase.Layouts
                    layout.Location = New Point(x, y)
                    Dim base2 As DesignerLayoutBase = [TryCast](layout,DesignerLayoutBase)
                    If (Not base2 Is Nothing) Then
                        base2.ActivityDesigner.Location = layout.Location
                    End If
                    layout.OnLayoutPosition(graphics, designerTheme, ambientTheme)
                    y = (y + (layout.Size.Height + selectionSize.Height))
                Next
            End Sub

            Public Overrides Sub OnLayoutSize(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme, ByVal containerSize As Size)
                Dim selectionSize As Size = ambientTheme.SelectionSize
                Dim size2 As New Size
                Dim layout As Layout
                For Each layout In MyBase.Layouts
                    layout.OnLayoutSize(graphics, designerTheme, ambientTheme, size2)
                    size2.Height = (size2.Height + layout.Size.Height)
                    size2.Height = (size2.Height + selectionSize.Height)
                    Dim num As Integer = (layout.Size.Width + (2 * (selectionSize.Width + ambientTheme.Margin.Width)))
                    size2.Width = Math.Max(size2.Width, num)
                Next
                If (MyBase.Layouts.Count > 0) Then
                    size2.Height = (size2.Height + 8)
                End If
                Me.MinimumSize = size2
                Dim size3 As New Size { _
                    .Width = Math.Max(containerSize.Width, size2.Height), _
                    .Height = size2.Height _
                }
                MyBase.Size = size3
            End Sub

            Public Overrides Sub OnPaint(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                MyBase.OnPaint(graphics, designerTheme, ambientTheme)
                Dim activityDesigner As StateDesigner = DirectCast(MyBase.ActivityDesigner, StateDesigner)
                Dim parser As ContainedDesignersParser = activityDesigner._designersParser
                If ((((parser.EventDrivenDesigners.Count > 0) OrElse (parser.StateInitializationDesigners.Count > 0)) OrElse (parser.StateFinalizationDesigners.Count > 0)) AndAlso ((parser.StateDesigners.Count > 0) OrElse (parser.LeafStateDesigners.Count > 0))) Then
                    Dim bounds As Rectangle = MyBase.Bounds
                    graphics.DrawLine(designerTheme.BorderPen, bounds.Left, bounds.Bottom, bounds.Right, bounds.Bottom)
                End If
            End Sub

            Public Overrides Sub ResizeLayout(ByVal newSize As Size)
                MyBase.ResizeLayout(newSize)
                Dim width As Integer = (newSize.Width - &H10)
                Dim layout As Layout
                For Each layout In MyBase.Layouts
                    Dim size As Size = layout.Size
                    If (size.Width > width) Then
                        layout.ResizeLayout(New Size(width, size.Height))
                    End If
                Next
            End Sub


            ' Fields
            Friend Const EventDrivenPadding As Integer = 8
        End Class

        Private Class ImageLayout
            Inherits Layout
            ' Methods
            Public Sub New(ByVal activityDesigner As ActivityDesigner)
                MyBase.New(activityDesigner)
            End Sub

            Public Overrides Sub OnLayoutSize(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme, ByVal containerSize As Size)
                MyBase.OnLayoutSize(graphics, designerTheme, ambientTheme, containerSize)
                Dim theme As CompositeDesignerTheme = [TryCast](designerTheme,CompositeDesignerTheme)
                Dim empty As Size = Size.Empty
                If ((Not MyBase.ActivityDesigner.Image Is Nothing) AndAlso (Not theme Is Nothing)) Then
                    empty = designerTheme.ImageSize
                End If
                Me.MinimumSize = empty
                MyBase.Size = empty
            End Sub

            Public Overrides Sub OnPaint(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                Dim image As Image = MyBase.ActivityDesigner.Image
                If (Not image Is Nothing) Then
                    ActivityDesignerPaint.DrawImage(graphics, image, MyBase.Bounds, DesignerContentAlignment.Fill)
                End If
            End Sub

        End Class

        Friend MustInherit Class Layout
            ' Events
            Public Custom Event MouseDoubleClick As MouseEventHandler
            Public Custom Event MouseDown As MouseEventHandler
            Public Custom Event MouseEnter As EventHandler
            Public Custom Event MouseLeave As EventHandler
            Public Custom Event MouseMove As MouseEventHandler
            Public Custom Event MouseUp As MouseEventHandler

            ' Methods
            Public Sub New(ByVal activityDesigner As ActivityDesigner)
                Me._activityDesigner = activityDesigner
            End Sub

            Public Function GetLayout(ByVal designer As ActivityDesigner) As Layout
                If (Me.ActivityDesigner Is designer) Then
                    Return Me
                End If
                Dim layout As Layout
                For Each layout In Me.Layouts
                    Dim layout2 As Layout = layout.GetLayout(designer)
                    If (Not layout2 Is Nothing) Then
                        Return layout2
                    End If
                Next
                Return Nothing
            End Function

            Private Function GetLayoutAt(ByVal x As Integer, ByVal y As Integer) As Layout
                Dim layout As Layout
                For Each layout In Me._layouts
                    If layout.Bounds.Contains(x, y) Then
                        Return layout
                    End If
                Next
                If Me.Bounds.Contains(x, y) Then
                    Return Me
                End If
                Return Nothing
            End Function

            Public Overridable Function HitTest(ByVal point As Point) As HitTestInfo
                Dim nowhere As HitTestInfo = HitTestInfo.Nowhere
                If Me.Bounds.Contains(point) Then
                    Dim layoutAt As Layout = Me.GetLayoutAt(point.X, point.Y)
                    If (Not layoutAt Is Me) Then
                        nowhere = layoutAt.HitTest(point)
                    End If
                End If
                Return nowhere
            End Function

            Public Overridable Sub Invalidate()
                Dim service As WorkflowView = [TryCast](StateDesigner.GetService(Me._activityDesigner, GetType(WorkflowView)),WorkflowView)
                If (Not service Is Nothing) Then
                    service.InvalidateLogicalRectangle(Me.Bounds)
                End If
            End Sub

            Public Overridable Sub MoveLayout(ByVal newLocation As Point)
                If (newLocation <> Me._location) Then
                    Dim point As New Point((Me._location.X - newLocation.X), (Me._location.Y - newLocation.Y))
                    Dim layout As Layout
                    For Each layout In Me._layouts
                        Dim location As Point = layout.Location
                        Dim point3 As New Point((location.X - point.X), (location.Y - point.Y))
                        layout.MoveLayout(point3)
                    Next
                    Me._location = newLocation
                End If
            End Sub

            Public Overridable Sub OnLayoutPosition(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
            End Sub

            Public Overridable Sub OnLayoutSize(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme, ByVal containerSize As Size)
            End Sub

            Public Overridable Sub OnMouseDoubleClick(ByVal e As MouseEventArgs)
                If (Me.MouseOverLayout Is Me) Then
                    If (Not Me.MouseDoubleClick Is Nothing) Then
                        Me.MouseDoubleClick.Invoke(Me, e)
                    End If
                ElseIf (Not Me.MouseOverLayout Is Nothing) Then
                    Me.MouseOverLayout.OnMouseDoubleClick(e)
                End If
                Dim layoutAt As Layout = Me.GetLayoutAt(e.X, e.Y)
                Me.MouseOverLayout = layoutAt
            End Sub

            Public Overridable Sub OnMouseDown(ByVal e As MouseEventArgs)
                If (Me.MouseOverLayout Is Me) Then
                    If (Not Me.MouseDown Is Nothing) Then
                        Me.MouseDown.Invoke(Me, e)
                    End If
                ElseIf (Not Me.MouseOverLayout Is Nothing) Then
                    Me.MouseOverLayout.OnMouseDown(e)
                End If
            End Sub

            Public Overridable Sub OnMouseEnter()
                If (Not Me.MouseEnter Is Nothing) Then
                    Me.MouseEnter.Invoke(Me, EventArgs.Empty)
                End If
            End Sub

            Public Overridable Sub OnMouseLeave()
                If (Not Me.MouseOverLayout Is Me) Then
                    Me.MouseOverLayout = Nothing
                End If
                If (Not Me.MouseLeave Is Nothing) Then
                    Me.MouseLeave.Invoke(Me, EventArgs.Empty)
                End If
            End Sub

            Public Overridable Sub OnMouseMove(ByVal e As MouseEventArgs)
                Dim layoutAt As Layout = Me.GetLayoutAt(e.X, e.Y)
                If (Not layoutAt Is Me.MouseOverLayout) Then
                    Me.MouseOverLayout = layoutAt
                End If
                If (Me.MouseOverLayout Is Me) Then
                    If (Not Me.MouseMove Is Nothing) Then
                        Me.MouseMove.Invoke(Me, e)
                    End If
                ElseIf (Not Me.MouseOverLayout Is Nothing) Then
                    Me.MouseOverLayout.OnMouseMove(e)
                End If
            End Sub

            Public Overridable Sub OnMouseUp(ByVal e As MouseEventArgs)
                If (Me.MouseOverLayout Is Me) Then
                    If (Not Me.MouseUp Is Nothing) Then
                        Me.MouseUp.Invoke(Me, e)
                    End If
                ElseIf (Not Me.MouseOverLayout Is Nothing) Then
                    Me.MouseOverLayout.OnMouseUp(e)
                End If
                Dim layoutAt As Layout = Me.GetLayoutAt(e.X, e.Y)
                Me.MouseOverLayout = layoutAt
            End Sub

            Public Overridable Sub OnPaint(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                Dim layout As Layout
                For Each layout In Me._layouts
                    layout.OnPaint(graphics, designerTheme, ambientTheme)
                Next
            End Sub

            Public Overridable Sub ResizeLayout(ByVal newSize As Size)
                Me._size = newSize
            End Sub


            ' Properties
            Public ReadOnly Property ActivityDesigner As ActivityDesigner
                Get
                    Return Me._activityDesigner
                End Get
            End Property

            Public ReadOnly Property Bounds As Rectangle
                Get
                    Return New Rectangle(Me._location, Me._size)
                End Get
            End Property

            Public ReadOnly Property Layouts As List([Of] Layout)
                Get
                    Return Me._layouts
                End Get
            End Property

            Public Property Location As Point
                Get
                    Return Me._location
                End Get
                Set(ByVal value As Point)
                    Me._location = value
                End Set
            End Property

            Public Overridable Property MinimumSize As Size
                Get
                    Return Me._minimumSize
                End Get
                Set(ByVal value As Size)
                    Me._minimumSize = value
                End Set
            End Property

            Public Property MouseOverLayout As Layout
                Get
                    Return Me._mouseOverLayout
                End Get
                Set(ByVal value As Layout)
                    If (Not value Is Me._mouseOverLayout) Then
                        If (Not Me._mouseOverLayout Is Nothing) Then
                            Me._mouseOverLayout.OnMouseLeave
                        End If
                        Me._mouseOverLayout = value
                        If (Not value Is Nothing) Then
                            value.OnMouseEnter
                        End If
                    End If
                End Set
            End Property

            Public Property Size As Size
                Get
                    Return Me._size
                End Get
                Set(ByVal value As Size)
                    Me._size = value
                End Set
            End Property


            ' Fields
            Private _activityDesigner As ActivityDesigner
            Private _layouts As List([Of] Layout) = New List([Of] Layout)
            Private _location As Point
            Private _minimumSize As Size
            Private _mouseOverLayout As Layout
            Private _size As Size
        End Class

        Private Class LayoutSelectionGlyph
            Inherits SelectionGlyph
            ' Methods
            Public Sub New(ByVal layout As Layout)
                If (layout Is Nothing) Then
                    Throw New ArgumentNullException("layout")
                End If
                Me._layout = layout
            End Sub

            Public Overrides Function GetBounds(ByVal designer As ActivityDesigner, ByVal activated As Boolean) As Rectangle
                If (designer Is Nothing) Then
                    Throw New ArgumentNullException("designer")
                End If
                Return Me._layout.Bounds
            End Function


            ' Properties
            Public Overrides ReadOnly Property IsPrimarySelection As Boolean
                Get
                    Return True
                End Get
            End Property

            Public Overrides ReadOnly Property Priority As Integer
                Get
                    Return 0
                End Get
            End Property


            ' Fields
            Private _layout As Layout
        End Class

        <ProvideProperty("ActiveDesignerName", GetType(Activity))> _
        Private NotInheritable Class StateDesignerPropertyExtender
            Implements IExtenderProvider
            ' Methods
            <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DesignOnly(True), MergableProperty(False), Browsable(False)> _
            Public Function GetActiveDesignerName(ByVal activity As Activity) As String
                Dim qualifiedName As String = Nothing
                Dim designer As StateDesigner = DirectCast(StateDesigner.GetDesigner(activity), StateDesigner)
                If (Not designer Is Nothing) Then
                    Dim activeDesigner As ActivityDesigner = designer.ActiveDesigner
                    If (Not activeDesigner Is Nothing) Then
                        qualifiedName = activeDesigner.Activity.QualifiedName
                    End If
                End If
                Return qualifiedName
            End Function

            Public Sub SetActiveDesignerName(ByVal activity As Activity, ByVal activeDesignerName As String)
                Dim designer As StateDesigner = DirectCast(StateDesigner.GetDesigner(activity), StateDesigner)
                If (Not designer Is Nothing) Then
                    designer.SetActiveDesignerByName(activeDesignerName)
                End If
            End Sub

            Private Function System.ComponentModel.IExtenderProvider.CanExtend(ByVal extendee As Object) As Boolean Implements IExtenderProvider.CanExtend
                Dim flag As Boolean = False
                Dim activity As StateActivity = [TryCast](extendee,StateActivity)
                If ((Not activity Is Nothing) AndAlso TypeOf StateDesigner.GetDesigner(activity) Is StateDesigner) Then
                    flag = True
                End If
                Return flag
            End Function

        End Class

        Private Class StatesLayout
            Inherits Layout
            ' Methods
            Public Sub New(ByVal activityDesigner As ActivityDesigner, ByVal titleBarLayout As TitleBarLayout, ByVal eventHandlersLayout As EventHandlersLayout)
                MyBase.New(activityDesigner)
                Me._titleBarLayout = titleBarLayout
                MyBase.Layouts.Add(titleBarLayout)
                Me._eventHandlersLayout = eventHandlersLayout
                MyBase.Layouts.Add(eventHandlersLayout)
            End Sub

            Public Overrides Sub OnLayoutPosition(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                Me._titleBarLayout.Location = MyBase.Location
                Me._titleBarLayout.OnLayoutPosition(graphics, designerTheme, ambientTheme)
                Dim x As Integer = MyBase.Location.X
                Dim y As Integer = (Me._titleBarLayout.Bounds.Bottom + 1)
                Me._eventHandlersLayout.Location = New Point(x, y)
                Me._eventHandlersLayout.OnLayoutPosition(graphics, designerTheme, ambientTheme)
            End Sub

            Public Overrides Sub OnLayoutSize(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme, ByVal containerSize As Size)
                MyBase.OnLayoutSize(graphics, designerTheme, ambientTheme, containerSize)
                Dim theme As CompositeDesignerTheme = DirectCast(designerTheme, CompositeDesignerTheme)
                Dim size As Size = containerSize
                Me._titleBarLayout.OnLayoutSize(graphics, theme, ambientTheme, size)
                Me._eventHandlersLayout.OnLayoutSize(graphics, designerTheme, ambientTheme, size)
                Dim width As Integer = Math.Max(Me._titleBarLayout.MinimumSize.Width, Me._eventHandlersLayout.MinimumSize.Width)
                Dim height As Integer = (Me._titleBarLayout.MinimumSize.Height + Me._eventHandlersLayout.MinimumSize.Height)
                Me.MinimumSize = New Size(width, height)
                size.Width = Math.Max(width, size.Width)
                size.Height = Math.Max(height, size.Height)
                If Me.StateDesigner.NeedsAutoLayout Then
                    Dim num3 As Integer = size.Width
                    Dim num4 As Integer = ((Me._titleBarLayout.Size.Height + Me._eventHandlersLayout.Size.Height) + &H10)
                    Dim flag As Boolean = False
                    Dim designer As ActivityDesigner
                    For Each designer In Me.StateDesigner.ContainedDesigners
                        If (Me.StateDesigner.IsContainedDesignerVisible(designer) AndAlso TypeOf designer Is StateDesigner) Then
                            flag = True
                            num3 = Math.Max(num3, designer.Size.Width)
                            num4 = (num4 + (designer.Size.Height + &H10))
                        End If
                    Next
                    If flag Then
                        num4 = (num4 + &H20)
                    End If
                    size = New Size(num3, num4)
                End If
                Me._titleBarLayout.ResizeLayout(New Size(size.Width, Me._titleBarLayout.Size.Height))
                MyBase.Size = size
            End Sub

            Public Overrides Sub OnPaint(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                Dim path As GraphicsPath = StateMachineDesignerPaint.GetDesignerPath(MyBase.ActivityDesigner, MyBase.Bounds, designerTheme)
                Dim backgroundBrush As Brush = designerTheme.GetBackgroundBrush(MyBase.Bounds)
                graphics.FillPath(backgroundBrush, path)
                MyBase.OnPaint(graphics, designerTheme, ambientTheme)
                If ambientTheme.ShowDesignerBorder Then
                    graphics.DrawPath(designerTheme.BorderPen, path)
                End If
                If ((Me.StateDesigner.ContainedDesigners.Count = 0) AndAlso Not Me.StateDesigner.IsStateCustomActivity) Then
                    Dim location As New Point(MyBase.Location.X, Me._titleBarLayout.Bounds.Bottom)
                    Dim size As New Size(MyBase.Size.Width, (MyBase.Size.Height - Me._titleBarLayout.Bounds.Height))
                    Dim boundingRect As New Rectangle(location, size)
                    boundingRect.Inflate(-1, -1)
                    Dim state As StateActivity = DirectCast(Me.StateDesigner.Activity, StateActivity)
                    If (Not StateMachineHelpers.IsLeafState(state) OrElse Not StateMachineHelpers.IsCompletedState(state)) Then
                        If Me.StateDesigner.DragDropActive Then
                            [Using] brush2 As SolidBrush = New SolidBrush(Color.FromArgb(&H40, designerTheme.ForeColor))
                                ActivityDesignerPaint.DrawText(graphics, designerTheme.Font, Me.StateDesigner.HelpText, boundingRect, StringAlignment.Center, ambientTheme.TextQuality, brush2)
                                Return
                            End [Using]
                        End If
                        ActivityDesignerPaint.DrawText(graphics, designerTheme.Font, Me.StateDesigner.HelpText, boundingRect, StringAlignment.Center, ambientTheme.TextQuality, designerTheme.ForegroundBrush)
                    End If
                End If
            End Sub

            Public Overrides Sub ResizeLayout(ByVal newSize As Size)
                Me._eventHandlersLayout.ResizeLayout(New Size(newSize.Width, Me._eventHandlersLayout.Size.Height))
                Me._titleBarLayout.ResizeLayout(New Size(newSize.Width, Me._titleBarLayout.Size.Height))
                MyBase.ResizeLayout(newSize)
            End Sub


            ' Properties
            Public ReadOnly Property EventHandlersLayout As EventHandlersLayout
                Get
                    Return Me._eventHandlersLayout
                End Get
            End Property

            Public Overrides ReadOnly Property MinimumSize As Size
                Get
                    Dim minimumSize As Size = MyBase.MinimumSize
                    minimumSize.Width = Math.Max(minimumSize.Width, StatesLayout.RealMinimumSize.Width)
                    minimumSize.Height = Math.Max(minimumSize.Height, StatesLayout.RealMinimumSize.Height)
                    Return minimumSize
                End Get
            End Property

            Private ReadOnly Property StateDesigner As StateDesigner
                Get
                    Return DirectCast(MyBase.ActivityDesigner, StateDesigner)
                End Get
            End Property


            ' Fields
            Private _eventHandlersLayout As EventHandlersLayout
            Private _titleBarLayout As TitleBarLayout
            Private Shared ReadOnly RealMinimumSize As Size = New Size(160, 80)
            Private Const StatePadding As Integer = &H10
        End Class

        Private Class TextLayout
            Inherits Layout
            ' Methods
            Public Sub New(ByVal activityDesigner As ActivityDesigner)
                MyBase.New(activityDesigner)
            End Sub

            Public Overrides Sub OnLayoutSize(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme, ByVal containerSize As Size)
                MyBase.OnLayoutSize(graphics, designerTheme, ambientTheme, containerSize)
                Dim theme As CompositeDesignerTheme = [TryCast](designerTheme,CompositeDesignerTheme)
                Dim text As String = MyBase.ActivityDesigner.Text
                Dim empty As Size = Size.Empty
                If ((Not theme Is Nothing) AndAlso Not String.IsNullOrEmpty([text])) Then
                    empty = StateMachineDesignerPaint.MeasureString(graphics, theme.Font, [text], StringAlignment.Center, Size.Empty)
                End If
                Me.MinimumSize = empty
                MyBase.Size = empty
            End Sub

            Public Overrides Sub OnPaint(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                Dim text As String = MyBase.ActivityDesigner.Text
                If Not String.IsNullOrEmpty([text]) Then
                    Dim font As Font = designerTheme.Font
                    ActivityDesignerPaint.DrawText(graphics, font, [text], MyBase.Bounds, StringAlignment.Near, ambientTheme.TextQuality, designerTheme.ForegroundBrush)
                End If
            End Sub

        End Class

        Private Class TitleBarLayout
            Inherits Layout
            ' Methods
            Public Sub New(ByVal activityDesigner As ActivityDesigner)
                MyBase.New(activityDesigner)
                Me._textLayout = New TextLayout(activityDesigner)
                MyBase.Layouts.Add(Me._textLayout)
                Me._imageLayout = New ImageLayout(activityDesigner)
                MyBase.Layouts.Add(Me._imageLayout)
            End Sub

            Private Sub CalculateTextLayout()
                Dim margin As Size = WorkflowTheme.CurrentTheme.AmbientTheme.Margin
                Dim num As Integer = (Me._imageLayout.Bounds.Right + 4)
                Dim width As Integer = Me._textLayout.Size.Width
                Dim x As Integer = ((MyBase.Location.X + (MyBase.Size.Width / 2)) - (width / 2))
                If (x < num) Then
                    x = num
                End If
                If ((x + width) > (MyBase.Bounds.Right - margin.Width)) Then
                    width = ((MyBase.Bounds.Right - margin.Width) - x)
                End If
                Me._textLayout.Location = New Point(x, (MyBase.Location.Y + margin.Height))
                Me._textLayout.Size = New Size(width, Me._textLayout.Size.Height)
            End Sub

            Public Overrides Sub OnLayoutPosition(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                Dim margin As Size = WorkflowTheme.CurrentTheme.AmbientTheme.Margin
                Dim location As Point = MyBase.Location
                location.X = (location.X + margin.Width)
                location.Y = (location.Y + 2)
                Me._imageLayout.Location = location
                Me.CalculateTextLayout
            End Sub

            Public Overrides Sub OnLayoutSize(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme, ByVal containerSize As Size)
                MyBase.OnLayoutSize(graphics, designerTheme, ambientTheme, containerSize)
                Dim size As New Size
                Me._textLayout.OnLayoutSize(graphics, designerTheme, ambientTheme, Size.Empty)
                Me._imageLayout.OnLayoutSize(graphics, designerTheme, ambientTheme, Size.Empty)
                size.Width = ((((designerTheme.BorderWidth * 2) + 10) + Me._textLayout.Size.Width) + Me._imageLayout.Size.Width)
                size.Height = Math.Max(Me._textLayout.Size.Height, Me._imageLayout.Size.Height)
                size.Height = (size.Height + ((designerTheme.BorderWidth * 2) + 4))
                Me.MinimumSize = size
                Dim size2 As Size = size
                size2.Width = Math.Max(size.Width, containerSize.Width)
                MyBase.Size = size2
            End Sub

            Public Overrides Sub OnPaint(ByVal graphics As Graphics, ByVal designerTheme As ActivityDesignerTheme, ByVal ambientTheme As AmbientTheme)
                Dim empty As Color
                Dim color2 As Color
                Dim bounds As Rectangle = MyBase.Bounds
                Dim backgroundBrush As Brush = designerTheme.GetBackgroundBrush(MyBase.Bounds)
                graphics.FillRectangle(backgroundBrush, bounds)
                Dim state As StateActivity = DirectCast(MyBase.ActivityDesigner.Activity, StateActivity)
                If StateMachineHelpers.IsLeafState(state) Then
                    empty = Color.FromArgb(&H20, designerTheme.BorderColor)
                    color2 = Color.FromArgb(160, designerTheme.BorderColor)
                ElseIf StateMachineHelpers.IsRootState(state) Then
                    empty = Color.Empty
                    color2 = Color.FromArgb(&H80, designerTheme.BorderColor)
                Else
                    empty = Color.FromArgb(&H10, designerTheme.BorderColor)
                    color2 = Color.FromArgb(&H10, designerTheme.BorderColor)
                End If
                If ((bounds.Width > 0) AndAlso (bounds.Height > 0)) Then
                    [Using] brush2 As Brush = New LinearGradientBrush(bounds, empty, color2, LinearGradientMode.Vertical)
                        graphics.FillRectangle(brush2, bounds)
                        graphics.DrawLine(designerTheme.BorderPen, bounds.Left, bounds.Bottom, bounds.Right, bounds.Bottom)
                    End [Using]
                End If
                MyBase.OnPaint(graphics, designerTheme, ambientTheme)
            End Sub

            Public Overrides Sub ResizeLayout(ByVal newSize As Size)
                MyBase.ResizeLayout(newSize)
                Me.CalculateTextLayout
            End Sub


            ' Properties
            Public ReadOnly Property ImageLayout As ImageLayout
                Get
                    Return Me._imageLayout
                End Get
            End Property

            Public ReadOnly Property TextLayout As TextLayout
                Get
                    Return Me._textLayout
                End Get
            End Property


            ' Fields
            Private _imageLayout As ImageLayout
            Private _textLayout As TextLayout
            Private Const Padding As Integer = 4
        End Class

        Friend Class TransitionInfo
            ' Methods
            Friend Sub New(ByVal setState As SetStateActivity, ByVal eventHandler As CompositeActivity)
                If (setState Is Nothing) Then
                    Throw New ArgumentNullException("setState")
                End If
                If (eventHandler Is Nothing) Then
                    Throw New ArgumentNullException("eventHandler")
                End If
                Me._setState = setState
                Me._eventHandler = eventHandler
            End Sub

            Friend Function Matches(ByVal stateDesignerConnector As StateDesignerConnector) As Boolean
                If (stateDesignerConnector Is Nothing) Then
                    Throw New ArgumentNullException("stateDesignerConnector")
                End If
                If ((Me.Connector Is Nothing) OrElse (Not Me.Connector Is stateDesignerConnector)) Then
                    If (((Me.SetState Is Nothing) OrElse (Me.SourceState Is Nothing)) OrElse ((Me.TargetState Is Nothing) OrElse (Me.EventHandler Is Nothing))) Then
                        Return False
                    End If
                    If (Me.SetState.QualifiedName <> stateDesignerConnector.SetStateName) Then
                        Return False
                    End If
                    If (Me.SourceState.QualifiedName <> stateDesignerConnector.SourceStateName) Then
                        Return False
                    End If
                    If ((Me.TargetState.QualifiedName <> stateDesignerConnector.TargetStateName) OrElse (stateDesignerConnector.Target.AssociatedDesigner.Activity.QualifiedName <> stateDesignerConnector.TargetStateName)) Then
                        Return False
                    End If
                    If (Me.EventHandler.QualifiedName <> stateDesignerConnector.EventHandlerName) Then
                        Return False
                    End If
                End If
                Return True
            End Function

            Private Shared Sub ParseEventHandler(ByVal eventHandler As CompositeActivity, ByVal transitions As List([Of] TransitionInfo))
                Dim queue As New Queue([Of] Activity)
                queue.Enqueue(eventHandler)
                Do While (queue.Count > 0)
                    Dim activity As Activity = queue.Dequeue
                    Dim setState As SetStateActivity = [TryCast](activity,SetStateActivity)
                    If (Not setState Is Nothing) Then
                        Dim item As New TransitionInfo(setState, eventHandler)
                        transitions.Add(item)
                    Else
                        Dim activity3 As CompositeActivity = [TryCast](activity,CompositeActivity)
                        If (Not activity3 Is Nothing) Then
                            Dim activity4 As Activity
                            For Each activity4 In activity3.Activities
                                queue.Enqueue(activity4)
                            Next
                            [Continue] Do
                        End If
                    End If
                Loop
            End Sub

            Friend Shared Function ParseStateMachine(ByVal rootState As StateActivity) As ReadOnlyCollection([Of] TransitionInfo)
                Dim transitions As New List([Of] TransitionInfo)
                Dim dictionary As New Dictionary([Of] String, StateActivity)
                Dim queue As New Queue([Of] StateActivity)
                queue.Enqueue(rootState)
                Do While (queue.Count > 0)
                    Dim activity As StateActivity = queue.Dequeue
                    dictionary.Item(activity.QualifiedName) = activity
                    Dim activity2 As Activity
                    For Each activity2 In activity.Activities
                        Dim item As StateActivity = [TryCast](activity2,StateActivity)
                        If (item Is Nothing) Then
                            Dim eventHandler As CompositeActivity = [TryCast](activity2,CompositeActivity)
                            If (Not eventHandler Is Nothing) Then
                                TransitionInfo.ParseEventHandler(eventHandler, transitions)
                            End If
                        Else
                            queue.Enqueue(item)
                        End If
                    Next
                Loop
                Dim info As TransitionInfo
                For Each info In transitions
                    If Not String.IsNullOrEmpty(info.SetState.TargetStateName) Then
                        Dim activity5 As StateActivity
                        dictionary.TryGetValue(info.SetState.TargetStateName, activity5)
                        info.TargetState = activity5
                    End If
                Next
                Return transitions.AsReadOnly
            End Function


            ' Properties
            Friend Property Connector As StateDesignerConnector
                Get
                    Return Me._connector
                End Get
                Set(ByVal value As StateDesignerConnector)
                    Me._connector = value
                End Set
            End Property

            Friend ReadOnly Property EventHandler As CompositeActivity
                Get
                    Return Me._eventHandler
                End Get
            End Property

            Friend ReadOnly Property SetState As SetStateActivity
                Get
                    Return Me._setState
                End Get
            End Property

            Friend ReadOnly Property SourceState As StateActivity
                Get
                    If (Me._eventHandler Is Nothing) Then
                        Return Nothing
                    End If
                    Return [TryCast](Me._eventHandler.Parent,StateActivity)
                End Get
            End Property

            Friend Property TargetState As StateActivity
                Get
                    Return Me._targetState
                End Get
                Set(ByVal value As StateActivity)
                    Me._targetState = value
                End Set
            End Property


            ' Fields
            Private _connector As StateDesignerConnector
            Private _eventHandler As CompositeActivity
            Private _setState As SetStateActivity
            Private _targetState As StateActivity
        End Class
    End Class
End Namespace

